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.

9
  • 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 Commented Mar 20, 2018 at 18:17
  • Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. Commented Mar 20, 2018 at 18:32
  • @sytech tried that as well. not working. it just selects the source and destination. but dragging and dropping is not happening Commented Mar 20, 2018 at 18:48
  • What does "tried with robot" mean? Do you mean the tools available at robotframework.org, or something else? Commented Mar 20, 2018 at 18:52
  • You can try my solution Commented Mar 20, 2018 at 18:57

1 Answer 1

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

    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much but it didnt work . Its not even selecting the element.
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?
Hi @Chuchoo i tried this code to but not able to do so

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.