Drag and Drop not working..as my drag and drop area not having 'Frames' i didn't use switchTo() here.
Actions action= new Actions(driver);
//driver.switchTo().frame(0);
WebElement drag= driver.findElement(By.xpath("//div[contains(text(),'GEO')]"));
WebElement drop= driver.findElement(By.xpath("//div[@placeholder='Drag and Drop the subjects which needs evaluation.']"));
//action.doubleClick(drag);
Thread.sleep(3000);
action.dragAndDrop(drag, drop).perform();
Thread.sleep(3000);
Can anyone please help?
-
You are testing against Chromedriver? If so, try the same test(s) against Firefox. Chromedriver has a known issue with drag-and-drop and HTML5. My HTML5 related elements, I can only drag-n-drop in non-Chrome. github.com/SeleniumHQ/selenium/issues/6235Charlie Wilson– Charlie Wilson2019年02月23日 18:56:32 +00:00Commented Feb 23, 2019 at 18:56
-
I'm testing on firefoxAlbin K S– Albin K S2019年02月24日 05:33:57 +00:00Commented Feb 24, 2019 at 5:33
4 Answers 4
You try using build() . Consider the example :
action.dragAndDrop(drag, drop).build().perform();
OR
Try the below code :
new Actions(driver)
.moveToElement(sourceElement)
.pause(Duration.ofSeconds(1))
.clickAndHold(sourceElement)
.pause(Duration.ofSeconds(1))
.moveByOffset(2, 0)
.moveToElement(targetElement)
.moveByOffset(2,0)
.pause(Duration.ofSeconds(1))
.release().build().perform();
Hope it'll resolve the issue
Note: I am adding as an answer since I don't have enough reps to add comment. I will delete the answer if required once the requested change is made.
Albin, is it possible to share the URL where you are facing the issue so we can try ourselves?
Also, a small correction for @jatin khattar.
Calling build() is not necessary when using perform(). perform() will call build() method internally. As per the java docs for [Actions][1]
class:
void perform()
A convenience method for performing the actions without calling build() first.
Edit: Okay. Sharing the URL is not possible and its understandable. However, can you please share the behaviour when running the code? Any error messages? Or what is happening when the code is executed?
-
so sorry about the URL...it's our upcoming project so that I can't share with anybody..Is there anything else that I can do?Albin K S– Albin K S2019年02月23日 16:07:40 +00:00Commented Feb 23, 2019 at 16:07
Can you please try following :
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)
.moveToElement(otherElement)
.release(otherElement)
.build();
dragAndDrop.perform();
In IT industry various software testing solutions are provided by companies depending on application behavior.
Below is one of the solution for DragandDrop issue w.r.t co-ordinates of webelement.
int sourceWidth = element.getSize().getWidth();
int SourceHeight = element.getSize().getHeight();
int destinationWidth = destElement.getSize().getWidth();
int destinationHeight = destElement.getSize().getHeight();
Actions act = new Actions(driver);
act.moveToElement(element, (sourceWidth / 2), SourceHeight / 2).clickAndHold().build().perform();
act.moveToElement(destElement, (destinationWidth / 2) , (destinationHeight / 2)).release().build().perform();
Explore related questions
See similar questions with these tags.