Drag and Drop in Selenium

Profile picture for user arilio666

We can easily drag and drop elements from source to destination using the actions class in selenium.

act.dragAndDrop(source, target).perform();

Example:

For example purposes, we will be using this site


        WebDriverManager.chromedriver().setup();
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://demo.seleniumeasy.com/drag-and-drop-demo.html");
        driver.manage().window().maximize();
        Actions act = new Actions(driver);
        WebElement source = driver.findElement(By.xpath("//span[normalize-space()='Draggable 3']")); 
        WebElement target = driver.findElement(By.xpath("//div[@id='mydropzone']")); 
        act.dragAndDrop(source, target).build().perform();
        
  • In the source, we provided the XPath of the element we want to drag.
  • In the target, we have specified the XPath of the destination, the source element to be placed.
  • Finally, using actions dragAndDrop function, we did the drag-drop operation.