Drag and Drop In Selenium

This is a action performed by an user By mouse where user move an web element (Drag) and place (Drop) it to another place. This scenario can not be achieved by Selenium webdriver basics commands. Selenium Provide Actions Class to perform such kind of scenarios.

Manual Actions:

  • Launch the web browser and open the application
  • Find the required Drag-able element
  • Find the required Drop-able element
  • Drag the element and drop it
  • Close the browser

Selenium WebDriver Steps

package automationTestingLab;

import java.util.concurrent.TimeUnit;

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;

public class DragAndDrop {

	public static void main(String[] args) throws InterruptedException {

		System.setProperty("webdriver.chrome.driver","F:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
		
		driver.get("https://jqueryui.com/droppable/");
		
		// switch to frame
		driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[class='demo-frame']")));
		
		//To get source locator
		WebElement source = driver.findElement(By.id("draggable"));
		
	    //To get target locator
		WebElement target = driver.findElement(By.id("droppable"));

	    //Create an object 'action'
		Actions a = new Actions(driver);
		
		// Drag and Drop
		a.dragAndDrop(source, target).build().perform();
		
		Thread.sleep(3000);
		driver.close();		
	}
}

Next: Mouse Hover (Tool tip)

Leave a comment

Design a site like this with WordPress.com
Get started