How to perform Drag and Drop in Selenium Java

asked 1 year ago

Drag and drop: 

To drag and drop the element on a web page, the selenium web driver provides an Action class.

We can use the drag and drop method of Actions class and pass the parameters as the first element(Sourcelocator) "From" and the second element(Destinationlocator) "To". 

Navigate to https://www.qafeast.com/demo,click the drag and drop tab.

Press F12 in the keyboard and inspect drag and drop.

drag_drop_selenium Java

 

Syntax:

(new Actions(driver)).dragAndDrop(from, to).perform();

Example:
WebElement element = driver.findElement(By.xpath("//p[text()='Python']"));
WebElement target = driver.findElement(By.xpath("//p[text()='Python.exe']"));
(new Actions(driver)).dragAndDrop(element, target).perform();