0

URL - http://www.seleniumeasy.com/test/drag-and-drop-demo.html

System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"//drivers//chrome//chromedriver.exe");
WebDriver driver=new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("http://www.seleniumeasy.com/test/drag-and-drop-demo.html");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    WebElement itemToBeDragged=driver.findElement(By.xpath(" (//h3[contains(.,'Items to Drag')]//following-sibling::span)[1]"));
WebElement 
   whereToBeDragged=driver.findElement(By.xpath("//div[@id='mydropzone']"));
    Actions action=new Actions(driver);
    Action dragAndDrop = action.clickAndHold(itemToBeDragged).moveToElement(whereToBeDragged).release(whereToBeDragged).build();
    dragAndDrop.perform();
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    driver.close();

Using above code I am not able to drag and drop the element. Please help !

2 Answers2

0

Can you try to use builder

Implement this method

    public static void dragAndDrop(WebElement fromWebElement, WebElement toWebElement) {

    Actions builder = new Actions(driver);
    builder.dragAndDrop(fromWebElement, toWebElement);
}
mbn217
  • 884
  • 7
  • 14
  • I have already tried with this method. But it's also not working – Manoj Joshi May 22 '18 at 15:22
  • ok can you try this method public static void dragAndDrop_Method2(WebElement fromWebElement, WebElement toWebElement) { log.info("Dragging and dropping an element"); Actions builder = new Actions(driver); Action dragAndDrop = builder.clickAndHold(fromWebElement).moveToElement(toWebElement).release(toWebElement) .build(); dragAndDrop.perform(); } – mbn217 May 23 '18 at 19:45
0

There is issue with the implementation of the Drag and Drop on the url:- http://www.seleniumeasy.com/test/drag-and-drop-demo.html, They might have added the drag and drop functionality by using hover events of the elements.

  1. As a result, unless or until we select and hover the itemToBeDragged for a second or more, it doesn't add the element to drag list,
  2. and in the same way unless or until we don't hover the selected itemToBeDragged over (hover) the dropzone, it doesn't drop the dragged element.

Therefore for this particular situation you can use Robot class of java to generate actual Mouse Events to perform drag and drop on this page as following:-

WebDriver browser = new ChromeDriver();
browser.get("http://www.seleniumeasy.com/test/drag-and-drop-demo.html");

// Robot class uses Screen Coordinates, therefore, we need the fullscreen browser 
// instead of maximised. so that the webpage coordinates are mapped to screen
// coordinates.
browser.manage().window().fullscreen();

WebElement itemToBeDragged = browser
            .findElement(By.xpath(" (//h3[contains(.,'Items to Drag')]//following-sibling::span)[1]"));
WebElement whereToBeDragged = browser.findElement(By.xpath("//div[@id='mydropzone']"));

// robot class object to perform actual mouse events.
// import java.awt.Robot and java.awt.event.InputEvent
Robot robot = new Robot();

try {
        robot.mouseMove(itemToBeDragged.getLocation().x+itemToBeDragged.getSize().width/2, itemToBeDragged.getLocation().y+itemToBeDragged.getSize().getHeight()/2);
        Thread.sleep(1000);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(itemToBeDragged.getLocation().x+itemToBeDragged.getSize().width/3, itemToBeDragged.getLocation().y+itemToBeDragged.getSize().getHeight()/3);
        Thread.sleep(1000);


        Thread.sleep(1000);
        robot.mouseMove(whereToBeDragged.getLocation().x+whereToBeDragged.getSize().width/3, whereToBeDragged.getLocation().y+whereToBeDragged.getSize().getHeight()/3);

        Thread.sleep(1000);
        robot.mouseMove(whereToBeDragged.getLocation().x+whereToBeDragged.getSize().width/2, whereToBeDragged.getLocation().y+whereToBeDragged.getSize().getHeight()/2);

        Thread.sleep(1000);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);

        Thread.sleep(1000);
    } catch (Exception e) {
        e.printStackTrace();
    }
nandal
  • 2,544
  • 1
  • 18
  • 23