How to Drag and Drop an Element using Selenium WebDriver in Java?
Last Updated :
28 Apr, 2025
Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the selenium provides a class called Actions. The Action class provides the method for drag and drop actions and many other mouse actions and Keyboard actions. The actions provided by this class are performed by an API called Advanced user interaction in the selenium web driver.
Let's discuss moving the cursor pointer using the Action class in the selenium web driver:
On some web pages, we need to perform the drag and drop functionality, in that case, we need to use the method that is provided by the actions class to perform this action. To work with the Actions class, first, we need to declare the actions class and import it "import org.openqa.selenium.interactions.Actions;".
Actions action=new Actions(driver);
To perform the drag and drop, the Actions class provides the method,
action.dragAndDrop(Source, Destination);
This method takes two input parameters, the first parameter is for the source location, and the second is for the destination location.
Example Program:
In this example, we navigate to a URL and perform the drag-and-drop action on that page.
Java
public class Geeks {
public void geeksforgeeks()
{
// set the chromedriver.exe path
// Please note that with SeleniumManager release
// in 4.6.0, it's not required to use any driver,
// Selenium should automatically handle everything.
// change the chromedriver.exe path accordingly if
// needed.
System.setProperty(
"webdriver.chrome.driver",
System.getProperty("user.dir")
+ "\\src\\test\\resources\\drivers\\chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(
"https://jqueryui.com/resources/demos/droppable/default.html");
// Navigate to URL
Thread.sleep(3000);
try {
Actions action = new Actions(driver);
WebElement drag
= driver.findElement(By.id("draggable"));
WebElement drop
= driver.findElement(By.id("droppable"));
action.dragAndDrop(drag, drop)
.build()
.perform();
Thread.sleep(3000);
}
catch (Exception e) {
System.out.println(
"Exception occurred while performing Drag and Drop : "
+ e)
}
driver.close();
}
}
In this program, we saved the Web Element of the draggable location in drag and droppable in drop and performed the drag and drop action using the Actions class. This is the output of the program.
Output:
Similar Reads
Best way to get element by XPath using JavaScript in Selenium WebDriver? Finding Selenium WebDriver elements requires the use of the XPath language, which is used to navigate XML documents. It provides a flexible and efficient method to locate website items. This article focuses on discussing how to get an element by XPath using JavaScript in Selenium WebDriver. JavaScri
3 min read
How to Take a Screenshot in Selenium WebDriver Using Java? Selenium WebDriver is a collection of open-source APIs used to automate a web application's testing. To capture a screenshot in Selenium, one must utilize the Takes Screenshot method. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used t
3 min read
How to Select Multiple Elements using Actions Class in Selenium using Java? Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a web page that has file upload or other functionality which requires selecting multiple elements, to perform the multiple select actions the selenium provides a class called Acti
2 min read
How to run Selenium Running Test on Chrome using WebDriver Selenium is a popular open-source tool for automating web browser interactions. It works with different web browsers like Chrome, Firefox, and more, and different programming languages like Python or Java to write tests. What is a Selenium ChromeDriver?ChromeDriver is a separate executable that Sele
3 min read
How to check if an element exists with Selenium WebDriver? Selenium is one of the most popular tools for automating web applications. Selenium is not a single tool but rather a set of tools that helps QA testers and developers to automate web applications. It is widely used to automate user interactions on a web page like filling out web forms, clicking on
7 min read
How to use xPath in Selenium WebDriver to grab SVG elements? Selenium WebDriver is a powerful tool for automating web browsers to test the functionality of web applications. However, when working with SVG elements on a webpage, it can be tricky to locate and interact with them using standard locators like ID, class, or name. In such cases, XPath comes to the
2 min read