1

drag and drop not working on selenium 3.8.

Here is the JS code for the element

This is what I have tried. But it is only selecting the element. Hold and drop is not happening.

WebElement source = driver.findElement(By.xpath("//tbody[@class ='lt-body']//tr[@data-test-id='table-row-id-20']//td[contains(@id,'ember')]//div[contains(@id,'ember')]//*[name()='svg']//*[name()='ellipse']"));
WebElement destination = driver.findElement(By.xpath("//tbody[@class ='lt-body']//tr[@data-test-id='table-row-id-3']//td[contains(@id,'ember')]//div[contains(@id,'ember')]//*[name()='svg']//*[name()='ellipse']"));

Actions builder = new Actions(driver);
int x2 =destination.getLocation().getX();
int y2 = destination.getLocation().getY();
builder.clickAndHold(source);
builder.moveByOffset(x2,y2);
builder.moveToElement(destination);
builder.release();
builder.perform();

Tried this one as well.

builder.clickAndHold(source).moveByOffset(x2,y2).moveToElement(destination).release().build().perform();

Tried with Robot as well. Everytime I can only see both source and destination element getting selected. But not dragged and dropped.

Also tried dragAndDrop of ActionsChain. That too didn't work.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    'drag and drop' should have its own method for actionchains. I would just use that instead of trying to get the same behavior by combining other actions. see [the reference](http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html#dragAndDropBy-org.openqa.selenium.WebElement-int-int-) – sytech Mar 20 '18 at 18:17
  • Please read why a [screenshot of HTML or code or error is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. – undetected Selenium Mar 20 '18 at 18:32
  • @sytech tried that as well. not working. it just selects the source and destination. but dragging and dropping is not happening – user9218893 Mar 20 '18 at 18:48
  • What does "tried with robot" mean? Do you mean the tools available at robotframework.org, or something else? – Bryan Oakley Mar 20 '18 at 18:52
  • You can try [my solution](https://stackoverflow.com/questions/48775864/click-and-drag-selenium-chrome-webdriver-is-not-dragging-but-will-click-and-h/48781939#48781939) – Fenio Mar 20 '18 at 18:57
  • Just a query looking at the html. There is an attribute "draggable=false" on the tr tag. Is there a class making this true in the element hierarchy below it? – Grasshopper Mar 20 '18 at 19:05
  • @Grasshopper on hovering the svg element, it will change to true. – user9218893 Mar 20 '18 at 19:06
  • @RafałLaskowski Tried but didnt work. Same issue. App is built on Ember framework. Is that an issue? It is just selecting the source and destination. but not dragging the source to the destination. – user9218893 Mar 20 '18 at 19:07
  • @BryanOakley Robotframework. – user9218893 Mar 20 '18 at 19:08

1 Answers1

1

Use the following javascript based method.This is very neat way to drag and drop provided two elements are given. This works almost every time. Good luck.

public void dragAndDrop(WebElement from, WebElement to) {
        js.executeScript("function createEvent(typeOfEvent) {\n" + "var event =document.createEvent(\"CustomEvent\");\n"
                + "event.initCustomEvent(typeOfEvent,true, true, null);\n" + "event.dataTransfer = {\n" + "data: {},\n"
                + "setData: function (key, value) {\n" + "this.data[key] = value;\n" + "},\n"
                + "getData: function (key) {\n" + "return this.data[key];\n" + "}\n" + "};\n" + "return event;\n"
                + "}\n" + "\n" + "function dispatchEvent(element, event,transferData) {\n"
                + "if (transferData !== undefined) {\n" + "event.dataTransfer = transferData;\n" + "}\n"
                + "if (element.dispatchEvent) {\n" + "element.dispatchEvent(event);\n"
                + "} else if (element.fireEvent) {\n" + "element.fireEvent(\"on\" + event.type, event);\n" + "}\n"
                + "}\n" + "\n" + "function simulateHTML5DragAndDrop(element, destination) {\n"
                + "var dragStartEvent =createEvent('dragstart');\n" + "dispatchEvent(element, dragStartEvent);\n"
                + "var dropEvent = createEvent('drop');\n"
                + "dispatchEvent(destination, dropEvent,dragStartEvent.dataTransfer);\n"
                + "var dragEndEvent = createEvent('dragend');\n"
                + "dispatchEvent(element, dragEndEvent,dropEvent.dataTransfer);\n" + "}\n" + "\n"
                + "var source = arguments[0];\n" + "var destination = arguments[1];\n"
                + "simulateHTML5DragAndDrop(source,destination);", from, to);

    }
Chuchoo
  • 823
  • 2
  • 17
  • 36
  • Thank you so much but it didnt work . Its not even selecting the element. – user9218893 Mar 21 '18 at 18:41
  • Just wondering, did you declared 'js' by doing something like following. public static JavascriptExecutor js; Then provide source and destination WebElement as parameters to the above method. Can you show the code how you did it? – Chuchoo Mar 25 '18 at 15:40
  • Hi @Chuchoo i tried this code to but not able to do so – Mani Apr 20 '20 at 04:21