
Drag and drop is a common web interaction where a user clicks an element, holds it, and moves it to a target. Automating this in Selenium WebDriver lets testers verify workflows involving sliders, lists, file uploads, and interactive dashboards. Accurate automation ensures the UI behaves correctly under test conditions.
Selenium provides methods to simulate these actions using the Actions class, which can click, hold, move, and release elements like a real user. HTML5 drag and drop often needs a more advanced approach because standard methods may fail across browsers.
This article covers how to perform drag and drop in Selenium WebDriver, including techniques, common issues, and best practices.
Drag and drop in Selenium refers to automating the movement of a web element from one location to another on a web page. It mimics the user action of clicking an element, holding it, moving it, and releasing it at a target position. This interaction is widely used in applications with interactive lists, dashboards, file uploads, sliders, and game interfaces.
Selenium WebDriver supports this through methods that replicate user gestures. The most common approach uses the Actions class to perform click-and-hold, move, and release sequences. For HTML5 elements, additional techniques such as JavaScript execution may be required because native drag-and-drop events may not fire correctly across all browsers.
Drag and drop interactions are common in modern web applications, but automating them is not always straightforward. These interactions often involve dynamic element positions, overlapping layers, or custom JavaScript events. Below are real-world scenarios where drag and drop automation is essential, with deeper technical context:

Performing drag and drop in Selenium involves simulating user interactions while ensuring the underlying events fire correctly. The challenge is that different web elements and frameworks handle drag-and-drop events differently. Standard approaches may fail with HTML5 elements or dynamically positioned items, so understanding the mechanics is crucial.
There are three main approaches to performing drag and drop in Selenium:
1. Using the Actions Class
Selenium’s Actions class can chain mouse events to replicate drag-and-drop behavior. For example, clickAndHold(source).moveToElement(target).release().perform() simulates picking up an element, moving it, and dropping it. This works for most standard web elements, but may fail if the target uses complex JavaScript event handling or custom drag-and-drop libraries.
2. Using Offset Movements
When elements cannot be dropped directly onto a target, moving by a calculated offset can help. moveByOffset(x, y) lets you drag an element a specific number of pixels. This is useful for sliders or elements whose drop targets are not recognized by moveToElement.
3. Using JavaScript Execution
For HTML5 or canvas-based drag-and-drop elements, standard Selenium actions may not trigger the necessary events. Executing JavaScript to manually dispatch dragstart, dragover, and drop events ensures the element behaves as expected. This method provides control over the sequence of events and is more reliable for advanced interactions.
Note: Each method has trade-offs. The Actions class is simple but sometimes unreliable with custom front-end frameworks. Offset movements require precise calculations and may fail if layouts change. JavaScript execution is more complex but offers reliability for HTML5 and dynamic interfaces.
The Actions class in Selenium WebDriver provides a way to simulate complex user interactions, including drag-and-drop. It works by chaining low-level mouse actions like clickAndHold, moveToElement, and release to replicate real user behavior. This method is most effective for standard web elements but may fail with HTML5-based drag-and-drop implementations.
Below is an example of using the Actions class in Java:
// Locate source and target elements
WebElement source = driver.findElement(By.id("draggable"));
WebElement target = driver.findElement(By.id("droppable"));
// Create an Actions object
Actions actions = new Actions(driver);
// Perform drag and drop
actions.clickAndHold(source)
       .moveToElement(target)
       .release()
       .build()
       .perform();
Key points to note:
actions.clickAndHold(source)
     .moveByOffset(50, 0)
       .moveToElement(target)
       .release()
       .perform();
Using the Actions class provides a straightforward way to automate standard drag-and-drop interactions while maintaining a sequence similar to a real user. It also allows adding extra steps like pauses or offsets to handle dynamic layouts.
HTML5 drag and drop introduces a challenge for Selenium automation. Standard Actions class methods often fail because they do not trigger the underlying JavaScript drag events (dragstart, dragover, drop) that HTML5 relies on. These events are handled by the browser’s internal event model, so Selenium’s simulated mouse actions may not be recognized by the page.
To handle HTML5 drag and drop, testers typically use JavaScript execution to trigger the correct event sequence manually. This ensures that both the visual movement and event-based logic are validated.
Below is a practical example using JavaScript with Selenium WebDriver in Java:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement source = driver.findElement(By.id("drag-source"));
WebElement target = driver.findElement(By.id("drop-target"));
String script = 
"function triggerDragAndDrop(source, target) {" +
"  const dataTransfer = new DataTransfer();" +
"  const dragStartEvent = new DragEvent('dragstart', { dataTransfer });" +
"  source.dispatchEvent(dragStartEvent);" +
"  const dragOverEvent = new DragEvent('dragover', { dataTransfer });" +
"  target.dispatchEvent(dragOverEvent);" +
"  const dropEvent = new DragEvent('drop', { dataTransfer });" +
"  target.dispatchEvent(dropEvent);" +
"}" +
"triggerDragAndDrop(arguments[0], arguments[1]);";
js.executeScript(script, source, target);
This approach directly fires the drag-and-drop events in the browser’s DOM, bypassing the limitations of Selenium’s native mouse simulation.
Points to consider when using this method:
Using JavaScript for HTML5 drag and drop gives testers fine control over event flow and ensures that the logic behind dynamic UIs is tested, not just the visual motion of elements.
Automating drag and drop often fails when Selenium cannot correctly simulate the underlying browser behavior. The problem may appear as the element not moving, the target not responding, or tests passing visually but failing functionally. These failures usually occur due to differences in how browsers, frameworks, and event models handle drag-and-drop actions.
Here are some of the most frequent issues and what causes them:
Reliable drag-and-drop automation requires careful handling of timing, element visibility, and browser compatibility. A stable test should not just move elements visually but also confirm that the intended application logic executes as expected.
Below are best practices that improve consistency and maintainability in drag-and-drop tests:
Automating drag and drop in Selenium involves more than moving elements visually. It requires understanding browser event handling, element readiness, and framework behavior to ensure that both the action and the resulting application logic work as intended.
Running such tests across real browsers and devices is essential to validate consistent drag-and-drop behavior. With BrowserStack Automate, teams can execute Selenium tests on real environments, identify browser-specific issues early, and ensure interactive elements function reliably for every user scenario.
Run Selenium Tests on Cloud
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop