Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHow to Drag and Drop an Element using Selenium WebDriver in Java?

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

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 neveropen()
    {
 
        // 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(
 
        // 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:

 

RELATED ARTICLES

Most Popular

Recent Comments