Tom Martin
Tom Martin

Reputation: 53

Using Actions able to select element but not able drag element to particular location, because drop functionality is created on hover

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

Upvotes: 4

Views: 545

Answers (1)

Wilfred Clement
Wilfred Clement

Reputation: 2774

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

Upvotes: 4

Related Questions