3

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?

2
  • 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/6235 Feb 23, 2019 at 18:56
  • I'm testing on firefox
    – Albin K S
    Feb 24, 2019 at 5:33

4 Answers 4

0

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

2
  • I tried both ways but doesn't worked for me
    – Albin K S
    Feb 23, 2019 at 10:15
  • This answer should work but If its not work I added another answer for this method.
    – sayhan
    Feb 26, 2019 at 15:24
0

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?

1
  • 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
    Feb 23, 2019 at 16:07
0

Can you please try following :

Actions builder = new Actions(driver);

    Action dragAndDrop = builder.clickAndHold(someElement)
       .moveToElement(otherElement)
       .release(otherElement)
       .build();

    dragAndDrop.perform();
0

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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.