Automate drag and drop event in selenium web driver using java

Automate drag and drop event in selenium web driver using java, To perform drag and drop operation in selenium. We need to use Actions class and its method. Actions class in selenium.

Key Points for the task

Create an object of Actions class.

action.dragAndDrop(dragElement, dropElement).build().perform();

Call the method to perform an action.

action.dragAndDrop(dragElement, dropElement).build().perform();

Automate drag and drop event in selenium web driver with an example.

Task scenario:

  1.  Open browser.
  2. Open URL “http://jqueryui.com/resources/demos/droppable/default.html”.
  3. Drag an element from source.
  4. Drop the element to the destination.
import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;



public class DragAndDropExample {

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



System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver_win32\\chromedriver.exe");



WebDriver driver = new ChromeDriver();



// Open webpage

driver.get("http://jqueryui.com/resources/demos/droppable/default.html");



// Create an object of actions class

Actions act = new Actions(driver);



// find element which we need to drag(Source element)

WebElement dragElement = driver.findElement(By.xpath(".//*[@id='draggable']"));



// find element which we need to drop(destination element)

WebElement dropElement = driver.findElement(By.xpath(".//*[@id='droppable']"));



// Wait for 3 sec to check properly

Thread.sleep(3000);



// Perfrom drag and drop.

act.dragAndDrop(dragElement, dropElement).build().perform();

}



}



Add a Comment

Your email address will not be published. Required fields are marked *

six − 5 =