Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Drag and drop is a common interaction in web applications, and Selenium WebDriver in Java provides a way to automate this using the ‘Actions‘ class. Here’s a detailed explanation with examples on how to perform drag and drop actions using Selenium in Java.

Prerequisites

1. Set up Selenium WebDriver:

  • Ensure you have the necessary Selenium dependencies in your project. If you are using Maven, include the following in your ‘pom.xml‘:
				
					<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.x.x</version>
</dependency>

				
			

2. Import the required classes:

				
					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;

				
			

Example 1: Basic Drag and Drop

Let’s consider an example where we want to drag an element from one location and drop it into another location.

HTML Structure Example

				
					<div id="draggable" class="ui-widget-content">
    <p>Drag me to my target</p>
</div>

<div id="droppable" class="ui-widget-header">
    <p>Drop here</p>
</div>

				
			

Java Code

				
					public class DragAndDropExample {
    public static void main(String[] args) {
        // Set the path for the ChromeDriver
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        // Navigate to the web page
        driver.get("URL_OF_YOUR_WEB_PAGE");

        // Locate the elements
        WebElement sourceElement = driver.findElement(By.id("draggable"));
        WebElement targetElement = driver.findElement(By.id("droppable"));

        // Create an instance of Actions class
        Actions actions = new Actions(driver);

        // Perform drag and drop
        actions.dragAndDrop(sourceElement, targetElement).build().perform();

        // Close the browser
        driver.quit();
    }
}

				
			

Example 2: Drag and Drop by Offset

Sometimes you may need to drag an element by a certain offset rather than dropping it onto another element. This can be achieved using the dragAndDropBy method.

HTML Structure Example

				
					<div id="draggable" class="ui-widget-content" style="position: absolute; left: 50px; top: 50px;">
    <p>Drag me by offset</p>
</div>

				
			

Java Code

				
					public class DragAndDropByOffsetExample {
    public static void main(String[] args) {
        // Set the path for the ChromeDriver
        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();

        // Navigate to the web page
        driver.get("URL_OF_YOUR_WEB_PAGE");

        // Locate the element
        WebElement sourceElement = driver.findElement(By.id("draggable"));

        // Create an instance of Actions class
        Actions actions = new Actions(driver);

        // Perform drag and drop by offset
        actions.dragAndDropBy(sourceElement, 100, 50).build().perform();

        // Close the browser
        driver.quit();
    }
}

				
			

Explanation

  1. Setup and Initialization:

    • Set the path to the chromedriver executable using System.setProperty.
    • Initialize the WebDriver and navigate to the desired web page.
    • Maximize the browser window for better visibility.
  2. Locate Elements:

    • Use By locators to find the elements you want to interact with. For example, By.id("draggable") to locate the element to be dragged.
  3. Actions Class:

    • Create an instance of the Actions class, passing the WebDriver instance to its constructor.
    • Use the dragAndDrop method for dragging and dropping from one element to another.
    • Use the dragAndDropBy method for dragging an element by a specific offset.
    • Call build().perform() to execute the action.
  4. Closing the Browser:

    • After performing the actions, close the browser using driver.quit().

These examples should help you get started with drag and drop interactions using Selenium WebDriver in Java. Adjust the element locators and offsets as needed to fit your specific use case.

Scroll to Top