How to Drag and Drop in Selenium?

Related Product On This Page Introduction to Drag and Drop in Selenium WebDriverJune 24, 2026 · 4 min read · Tool Comparison

Related Product

How to Drag and Drop in Selenium?

Selenium volunteer a wide range of functionalities for. One website characteristic that must be automatize during testing is Drag and Drop. provides an easy way to drag a web element from one piece of the website and driblet it at another.

Overview

Key Methods for Performing Drag and Drop in Selenium

  • dragAndDrop () Method: Simplifies drag-and-drop actions by directly moving elements.
  • clickAndHold (), moveToElement (), release ():Provides granular control for complex drag-and-drop scenarios.
  • build() and perform(): Chain and execute multiple user interactions in sequence.

Example of Automating Drag and Drop in Selenium

Using Selenium with Java, quizzer can drag one web element (e.g., file A) and drop it onto another (file B). By leverage theActions class, the automation ensures the interaction mimics real user behavior.

Business Impact of Automating Drag and Drop Testing

  • Improved Test Coverage: Validates critical UI workflows across browsers and platforms.
  • Reduced Manual Overhead: Automates repetitive drag-and-drop assay.
  • Faster Release Cycles: Detects UI issues early, ensuring quicker fixes.
  • Enhanced User Experience: Guarantees seamless interaction across devices.

This article discusses how to execute this function in Selenium WebDriver.

Introduction to Drag and Drop in Selenium WebDriver

Few web applications have the power to automate the functionality of drag and pearl, i.e. haul web elements from one end and drop them on a defined country or element. In order to automate the drag and drop activeness of such elements, one can use Selenium Webdriver.

What is Drag and Drop in Selenium?

This action is execute utilize amousewhen a exploitermotion (drags)a web element from one location and thenplaces (drops)it at another point.

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

This is a common activeness used in Windows Explorer when moving a file from one booklet to another. Here, the user select a file in the folder, drags it to the desire folder, and drops it.

Syntax:

Actions action = new Actions (driver); action.dragAndDrop (Sourcelocator, Destinationlocator) .build () .perform ();

In the SeleniumdragAndDropmethod, we pass two parameters:

  • The first parameter is theSourcelocatorelement which is being dragged
  • The second argument is theDestinationlocatoron which the previous component needs to bedroppedMethodsfor performing drag and driblet on a web constituent are as follows:
    • clickAndHold (WebElement factor)– Clicks a web element at the middle (without releasing)
    • moveToElement (WebElement element)– Moves the mouse pointer to the middle of the web element without clicking
    • liberation (WebElement element)– Releases the left chink (which is in the pressed state)
    • build()– Generates a composite action

Also Read:

Code Snippet

The codification snippet below demonstrates the automation of the Drag and Drop activeness program.

//WebElement on which drag and drop operation needs to be performed WebElement fromElement = driver.findElement (By Locator of fromElement); //WebElement to which the above object is dropped WebElement toElement = driver.findElement (By Locator of toElement); //Creating target of Actions form to build composite actions Actions builder = new Actions (driver); //Building a drag and drop activeness Action dragAndDrop = builder.clickAndHold (fromElement) .moveToElement (toElement) .release (toElement) .build (); //Performing the drag and drop action dragAndDrop.perform ();

Try Running Code on Cloud Selenium Grid for Free

Difference between Drag and Drop vs Action Class Build

Drag and DropAction Class Build
Selenium provides action.dragandDrop class to travel elements from one end to anotherUsed to move an element from one end to another by working on the element coordinates
The user does not have to use build () and perform () actions here to move the element which is an added advantagebuild() method in Actions classis used to create the chain ofactionor operation to be performed.perform() method in is used toexecutethe chain ofactionmake usingbuild () method

Use case: Example of Drag and Drop Action using Selenium and Java

In this example, the user will drag file A and drop it onto file B as testify below.

Read More:

Let ’ s now code the like example to see how it works.

meaning java.util.concurrent.TimeUnit; importation org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; meaning org.openqa.selenium.interactions.Actions; public class DragAndDropExample {public still void main (String [] args) throws InterruptedException {//Set system properties for geckodriver System.setProperty (`` webdriver.gecko.driver '', `` Path_of_the_driver ''); WebDriver driver = new FirefoxDriver (); String URL = `` https: //the-internet.herokuapp.com/drag_and_drop ''; driver.get (URL); // It is incessantly advisable to Maximize the window before performing DragNDrop action driver.manage () .window () .maximize (); driver.manage () .timeouts () .implicitlyWait (10000, TimeUnit.MILLISECONDS); //Actions class method to drag and drop Actions constructor = new Actions (driver); WebElement from = driver.findElement (By.id (`` column-a '')); WebElement to = driver.findElement (By.id (`` column-b '')); //Perform drag and drib builder.dragAndDrop (from, to) .perform (); //verify text changed in to 'Drop here ' box String textTo = to.getText (); if (textTo.equals (`` Dropped! '')) {System.out.println (`` PASS: File is dropped to target as ask '');} else {System.out.println (`` FAIL: File could n't be dropped to target as expected '');} driver.close ();}}

Try Testing Code on Cloud Selenium Grid for Free

Run the code, and see how to automate the Drag and Drop action in Selenium. This is a mutual feature of website and thus needs to be automatize during. This article simplifies the process and helps testers get part with it directly.

Tags
67,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free