Skip to content

Instantly share code, notes, and snippets.

@udayanem
Created February 6, 2015 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udayanem/2e15a421311cddbfa366 to your computer and use it in GitHub Desktop.
Save udayanem/2e15a421311cddbfa366 to your computer and use it in GitHub Desktop.
How to perform drag and drop operation using webdriver
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class DragAndDropExample
{
public static void main(String[] args) throws InterruptedException
{
WebDriver driver=new FirefoxDriver();
driver.get("http://test.cita.illinois.edu/aria/draganddrop/draganddrop3.php");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.findElement(By.id("startButton")).click();
Thread.sleep(3000);
WebElement target=driver.findElement(By.xpath("//td[@id='cell1x1']"));
WebElement source =driver.findElement(By.xpath("//div[@id='ex']/img"));
Actions action=new Actions(driver);
// action.clickAndHold(source).moveToElement(target).release(target).perform();
// action.clickAndHold(source).moveToElement(target).release(target).build().perform();
// action.clickAndHold(source).moveToElement(target).release().perform();
action.dragAndDrop(source, target).perform();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment