4

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

 System.setProperty("webdriver.chrome.driver", "D:\\Eclipse\\Files\\chromedriver_win32\\chromedriver.exe");
         driver = new ChromeDriver();   
      driver.manage().window().fullscreen();
         driver.get("http://www.seleniumeasy.com/test/drag-and-drop-demo.html");        
             Thread.sleep(5000);

         WebElement itemToBeDragged = driver.findElement(By.xpath("//div[@id='todrag']//span[3]"));
         WebElement whereToBeDragged = driver.findElement(By.xpath("//div[@id='mydropzone']"));

         Thread.sleep(3000);
         Actions builder = new Actions(driver);
         builder.clickAndHold(itemToBeDragged).moveToElement(whereToBeDragged).build();
         Thread.sleep(3000);
         builder.dragAndDrop(itemToBeDragged, whereToBeDragged).perform();

I already tried my solutions but none work for me. for ex.:-

  1. https://gist.github.com/rcorreia/2362544
  2. Java - drag and drop not working on selenium 3.8
  3. Not able to drag and drop element to another element using Selenium-Webdriver
  4. Not able to drag element using Actions
Tom Martin
  • 53
  • 5
  • looks like this page uses HTML5 events... you'll need to execute javascript to create them... see answer here: https://stackoverflow.com/questions/56604135/why-this-seleniums-drag-and-drop-c-sharp-code-is-not-working-on-chrome/56615037#56615037 For the "todrag" item, you'll need to use a different locator than ID. – pcalkins Apr 24 '20 at 21:04
  • hi @pcalkins thanks for the comment, i don't think so this will be helpful, reason on my problem element is droppable on hover . – Tom Martin Apr 26 '20 at 10:39

1 Answers1

4

Tried with most of the suggestions on SO and finally came up with this, I am very surprised cause drag and drop is achievable through a lot of ways but none of them seemed to work on this particular link, The below code seems to work fine ( Tried with all 4 draggables )

Used Robot class here

    driver.get("https://www.seleniumeasy.com/test/drag-and-drop-demo.html");

    driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);

    Point coordinates = driver.findElement(By.xpath("//div[@id='todrag']//span[3]")).getLocation();
    Point coordinatesa = driver.findElement(By.xpath("//*[@id='mydropzone']")).getLocation();
    Robot robot = new Robot();
    robot.mouseMove(coordinates.getX(), coordinates.getY() + 120);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseMove(coordinatesa.getX() + 100, coordinatesa.getY() + 130);
    Thread.sleep(500);
    robot.mouseMove(coordinatesa.getX() + 80, coordinatesa.getY() + 130);
    robot.delay(2000);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
Wilfred Clement
  • 2,674
  • 2
  • 14
  • 29
  • Hi @Wilfred Clement First Thanks for giving me answer But i am facing problem you solution is not working properly, when i run test case mouse cursor is selecting "Drag and Drop Demo for Automation Drag and Drop Items to Drag" not the actual elements May be this is the screen resolution issue Any Suggestion? – Tom Martin Apr 27 '20 at 05:41
  • Yup, That should be the case, I cannot think of a workaround at the moment and I left it as is since it was working, Try changing the coordinates. I will check for alternatives – Wilfred Clement Apr 27 '20 at 05:52
  • Can you please share your monitor screen resolution, version of browser and selenium webdriver so i make same to my side – Tom Martin Apr 27 '20 at 06:00
  • Can you please share your monitor screen resolution, version of browser and selenium webdriver so i make same to my side. – Tom Martin Apr 27 '20 at 06:06
  • Resolution - 1366x768 Browser - Chrome Version - Version 81.0.4044.122 (Official Build) (64-bit) Driver - ChromeDriver 81.0.4044.69 – Wilfred Clement Apr 27 '20 at 06:15
  • Thanks for the Answer and Help @Wilfred Clement – Mani Apr 28 '20 at 08:22