Published November 23, 2017 by

Drag And Drop In Selenium WebDriver

In some applications, we may face a situation to automate drag and drop an item from one location to another location.  Selenium has provided an “Actions” class to handle this kind of scenarios. 

Sample Script:

import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import;import org.openqa.selenium.interactions.Actions;

public class DragAndDrop {
 public static void main(String[] args) {
  System.setProperty("", "C://Program Files/eclipse/chromedriver_win32/chromedriver.exe");  WebDriver driver = new ChromeDriver();
  // driver.manage().window().maximize();
  WebElement source = driver.findElement(By.xpath("//*[@id='box6']"));  WebElement target = driver.findElement(By.xpath("//*[@id='box107']"));
  Actions act = new Actions(driver);
  // act.clickAndHold(source).moveToElement(target).release().build().perform(); // First method  act.dragAndDrop(source, target).build().perform(); // Second method


    email this