Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Drag and Drop an Element using Selenium WebDriver in Java?

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 12 Jan, 2023
Improve Article
Save Article

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() {
 
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
         
        // Navigate to URL
        Thread.sleep(3000);
       
        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);
        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:

 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!