2
  1. Drag and Drop Selenium

    WebElement source = driver.findElement(By.cssSelector(locator1));
    WebElement target = driver.findElement(By.cssSelector(locator2));
    Actions act = new Actions(driver);
    

(Option1)

     act.moveToElement(source) .pause(Duration.ofSeconds(1)) .clickAndHold(source)
      .pause(Duration.ofSeconds(1)) .moveByOffset(1, 0) .moveToElement(target)
     .moveByOffset(1, 0) .pause(Duration.ofSeconds(1)) .release().perform();
      System.out.println();
  1. Option2

    act.clickAndHold(source).pause(2000) .moveToElement(target) .release().build().perform();

I have tried with both option , I am working on Selenium 3, Is there any other way to achieve it.

Note: Robot Class not recommended

kaweesha
  • 803
  • 6
  • 16
Sri
  • 21
  • 1
  • 2
  • Does this answer your question? [Why drag and drop is not working in Selenium Webdriver?](https://stackoverflow.com/questions/39436870/why-drag-and-drop-is-not-working-in-selenium-webdriver) – kaweesha Jun 25 '20 at 08:59
  • Also check [this](https://stackoverflow.com/questions/62569044/webdriver-drag-and-drop-does-not-work-on-the-page/62569809#62569809) – Wilfred Clement Jun 25 '20 at 10:01

2 Answers2

3

This is drag and drop functionality using Actions

public void dragAndDrop(WebElement from, WebElement to) {
        Actions action = new Actions(driver);
        action.dragAndDrop(from,to).perform();
    }

This is using JSExecutor:

public void dragAndDrop(WebElement from, WebElement to) {
        JavascriptExecutor js = (JavascriptExecutor)driver;
        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);
    }
    

UPDATE:

        WebElement a = driver.findElement(By.cssSelector("a:nth-child(1)"));
        WebElement b = driver.findElement(By.cssSelector("a:nth-child(2)"));

        int x = b.getLocation().x;
        int y = b.getLocation().y;

        Actions actions = new Actions(driver);
        actions.moveToElement(a)
                .pause(Duration.ofSeconds(1))
                .clickAndHold(a)
                .pause(Duration.ofSeconds(1))
                .moveByOffset(x, y)
                .moveToElement(b)
                .moveByOffset(x,y)
                .pause(Duration.ofSeconds(1))
                .release().build().perform();
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
  • https://react-beautiful-dnd.netlify.app/iframe.html?id=single-vertical-list--basic Source Element----->"//*[text()='Sometimes life is scary and dark']", toElement ---->"//*[text()='Sucking at something is the first step towards being sorta good at something.']", Its not working on this site,Could you please help me. – Sri Jun 29 '20 at 07:42
  • Now it's working, I have tried different methods, this one is working, it's too long for a comment, I have updated my answer. Let me know about your result. – Norayr Sargsyan Jun 29 '20 at 09:15
  • its working for me too, but when tried here it is not working https://artoftesting.com/samplesiteforselenium – Sri Jul 02 '20 at 06:27
  • In this case, you can use the second part from my answer (using JSExecutor) – Norayr Sargsyan Jul 02 '20 at 06:35
1

I tried this one solution it's working fine on Selenium Automation.

The modified Code is here.

    WebElement a = driver.findElement(By.cssSelector(""));
    WebElement b = driver.findElement(By.cssSelector(""));

    int x = a.getLocation().x;
    System.out.println("Location of X : " +x);
    int y = a.getLocation().y;
    System.out.println("Location of Y : " +y);

    System.out.println("Location of A : " +a.getLocation().x + " " + a.getLocation().y);

    Actions actions = new Actions(driver);
    actions.moveToElement(a)
            .pause(Duration.ofSeconds(5))
            .clickAndHold(a)
            .pause(Duration.ofSeconds(10))
            .moveByOffset(x, y)
            .moveToElement(b)
            .moveByOffset(x,y)
            .pause(Duration.ofSeconds(5))
            .release().build().perform();`