Performing drag-and-drop operation

In Selenium WebDriver, dragAndDrop method available in Actions class. Actions class supports all advanced user interactions such as firing mouse and keyboard events on a web page.

Below is the sample logic to perform drag and drop operation on a page using Actions class.

Logic


?
1
2
3
4
5
WebElement elementToDrag = driver.findElement(By.id("DragThis"));
WebElement elementToDropAt = driver.findElement(By.id("DropHere"));
 
Actions action = new Actions(driver);
action.dragAndDrop(elementToDrag, elementToDropAt).perform();


dragAndDrop method in actions class accepts two parameters as input. One is the element to drag and another one is the destination to drop element.