Reputation: 53
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.:-
Upvotes: 4
Views: 545
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