Drag and Drop Action in Selenium WebDriver

Muntasir Abdullah Mizan
Oceanize Lab Geeks
Published in
2 min readDec 29, 2017

We have predefined method called dragAndDrop(source, destination) which is a method of Actions class.

Selenium

Approach- Find the xpath of the Source and find the xpath of destination.

Both source and destination in form of WebElement.

Note- Any method of Actions class we need to call perform () method otherwise we will get an exception. If we have series of action in our script using Actions class then we have to call build().perform() method.

publicclassDragDrop {

public static WebDriver driver=new FirefoxDriver();

public String baseurl=”http://www.abcxyz.html";

public void open_url()

{

driver.get(baseurl);

driver.manage().window().maximize();

}

public void action_1() throws InterruptedException

{

Thread.sleep(500);

driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);

driver.findElement(By.xpath(“.//*[@id=’box1']”)).click();

WebElement drag=driver.findElement(By.xpath(“.//*[@id=’box1']”));

WebElement drop =driver.findElement(By.xpath(“.//*[@id=’box101']”));

Actions builder= newActions(driver);

builder.dragAndDrop(drag, drop).perform();

builder.build();

}

public void action_2() throws InterruptedException

{

Thread.sleep(500);

driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);

driver.findElement(By.xpath(“.//*[@id=’box4']”)).click();

WebElement drag=driver.findElement(By.xpath(“.//*[@id=’box4']”));

WebElement drop =driver.findElement(By.xpath(“.//*[@id=’box104']”));

Actions builder= newActions(driver);

builder.dragAndDrop(drag, drop).perform();

builder.build();

}

public void action_3() throws InterruptedException

{

Thread.sleep(500);

driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);

driver.findElement(By.xpath(“.//*[@id=’box2']”)).click();

WebElement drag=driver.findElement(By.xpath(“.//*[@id=’box2']”));

WebElement drop =driver.findElement(By.xpath(“.//*[@id=’box102']”));

Actions builder= newActions(driver);

builder.dragAndDrop(drag, drop).perform();

builder.build();

}

public void action_4() throws InterruptedException

{

Thread.sleep(500);

driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);

driver.findElement(By.xpath(“.//*[@id=’box3']”)).click();

WebElement drag=driver.findElement(By.xpath(“.//*[@id=’box3']”));

WebElement drop =driver.findElement(By.xpath(“.//*[@id=’box103']”));

Actions builder= newActions(driver);

builder.dragAndDrop(drag, drop).perform();

builder.build();

}

public void action_5() throws InterruptedException

{

Thread.sleep(500);

driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);

driver.findElement(By.xpath(“.//*[@id=’box5']”)).click();

WebElement drag=driver.findElement(By.xpath(“.//*[@id=’box5']”));

WebElement drop =driver.findElement(By.xpath(“.//*[@id=’box105']”));

Actions builder= newActions(driver);

builder.dragAndDrop(drag, drop).perform();

builder.build();

}

public void action_6() throws InterruptedException

{

Thread.sleep(500);

driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);

driver.findElement(By.xpath(“.//*[@id=’box7']”)).click();

WebElement drag=driver.findElement(By.xpath(“.//*[@id=’box7']”));

WebElement drop =driver.findElement(By.xpath(“.//*[@id=’box107']”));

Actions builder= newActions(driver);

builder.dragAndDrop(drag, drop).perform();

builder.build();

}

public void action_7() throws InterruptedException

{

Thread.sleep(500);

driver.manage().timeouts().implicitlyWait(5000, TimeUnit.SECONDS);

driver.findElement(By.xpath(“.//*[@id=’box6']”)).click();

WebElement drag=driver.findElement(By.xpath(“.//*[@id=’box6']”));

WebElement drop =driver.findElement(By.xpath(“.//*[@id=’box106']”));

Actions builder= newActions(driver);

builder.dragAndDrop(drag, drop).perform();

builder.build();

}

public static void main(String[] args) throws InterruptedException {

DragDrop test=new DragDrop();

test.open_url();

test.action_1();

test.action_2();

test.action_3();

test.action_4();

test.action_5();

test.action_6();

test.action_7();

driver.close();

}

--

--