How do you perform drag and drop operations in WebDriver?

How do you perform drag and drop operations in WebDriver?

Hi Dipen,

You can use below code snippet to perform drag and drop:

//WebElement on which drag and drop operation needs to be performed

WebElementfromWebElement = driver.findElement(By Locator of fromWebElement);

//WebElement to which the above object is dropped

WebElementtoWebElement = driver.findElement(By Locator of toWebElement);

//Creating object of Actions class to build composite actions

Actions builder = newActions(driver);

//Building a drag and drop action

Action dragAndDrop = builder.clickAndHold(fromWebElement)

.moveToElement(toWebElement)

.release(toWebElement)

.build();

//Performing the drag and drop action

dragAndDrop.perform();```