top of page

Selenium Java Tutorial

WebDriver-Drag And Drop

What Does Selenium Provide For Drag And Drop Actions ?

Some of the web applications have functionality to drag a specific webelement and drop on a specific webelement. Drag and drop can be automated using selenium webdriver. ​ To achieve such automation, selenium provides Actions class. The Action class has two methods that performs drag and drop in different ways, methods are :
  • dragAndDrop(SourceLocator,DestinationLocator);

  • dragAndDropBy(SourceLocator,x-axis pixel of DestinationLocator,y-axis pixel of DestionationLocator);


​ SourceLocator
It is a location(locator) of webelement which have to be dragged. ​
DestinationLocator
It is a location(locator) on which the dragged webelement have to be dropped.
For automating drag and drop first we have to import Action class.

import org.openqa.selenium.interactions.Actions;

Now create an object for Actions class.

Actions action=new Actions(driver);

Using dragAndDrop() method
This is a parameterized method and accepts only WebElement as a parameter.

action.dragAndDrop(SourceLocator1, DestinationLocator1).build().perform();

​ Using dragAndDropBy() method
This is also a parameterized method that accepts one WebElement and to integer as a parameter.

action.dragAndDropBy(SourceLocator2,x,y).build().perform();

* Here build() and perform() are used to perform the required action. Here is a complete code for drag and drop =>

​package seleniumexamples; 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 DragAndDropExample { void toVerify(String ActualText,String ExpectedText,String ElementName) { if(ExpectedText.equals(ActualText)) System.out.println("PASS! Dragged Element '"+ElementName+"' is Dropped at Target Location !!!"); else System.out.println("FAIL! Element '"+ElementName+"' is not Dropped at Target Location !!!"); }

public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub WebDriver driver; String baseURL="https://demo.guru99.com/test/drag_drop.html"; String ActualText,ExpectedText,ElementName; DragAndDropExample draganddrop=new DragAndDropExample(); //Launch the web browser... System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe"); driver=new ChromeDriver(); //To maximize the size of window... driver.manage().window().maximize(); //Navigate through an URL.. driver.get(baseURL); //Instantiate Actions class to access dragAndDrop() and dragAndDropBy() methods... Actions action=new Actions(driver); // 1. By using dragDrop() method... System.out.println("\nPerforming Drag and Drop with dropAndDown() Method ..."); WebElement SourceLocator1=driver.findElement(By.id ("credit2")); WebElement DestinationLocator1=driver.findElement(By.xpath("//ol[@id='bank']")); //Invoke dragAndDrop() method... action.dragAndDrop(SourceLocator1, DestinationLocator1).build().perform(); //to verify the operation... ActualText="BANK"; ElementName=ActualText; ExpectedText=DestinationLocator1.getText(); draganddrop.toVerify(ActualText,ExpectedText,ElementName);

// 2. By using dragAndDropBy() method... System.out.println("\nPerforming Drag and Drop with dropAndDownBy() Method ..."); WebElement SourceLocator2=driver.findElement(By.id ("credit1")); WebElement DestinationLocator2=driver.findElement(By.xpath("//ol[@id='loan']")); int x1=SourceLocator2.getLocation().getX(); int y1=SourceLocator2.getLocation().getY(); int x=DestinationLocator2.getLocation().getX(); int y=DestinationLocator2.getLocation().getY(); System.out.println("Pixel along x-axis of source : "+x1); System.out.println("Pixel along y-axis of source : "+y1); System.out.println("Pixel along x-axis of destination : "+x); System.out.println("Pixel along y-axis of destination : "+y); x=x-x1; y=y-y1; //Invoke the method dragAndDropBy... action.dragAndDropBy(SourceLocator2,x,y).build().perform(); //to verify the operation... ActualText="SALES"; ElementName=ActualText; ExpectedText=DestinationLocator2.getText(); draganddrop.toVerify(ActualText,ExpectedText,ElementName); Thread.sleep(5000); driver.quit(); } }

Output :

Performing Drag and Drop with dropAndDown() Method ... PASS! Dragged Element 'BANK' is Dropped at Target Location !!! Performing Drag and Drop with dropAndDownBy() Method ... Pixel along x-axis of source : 689 Pixel along y-axis of source : 360 Pixel along x-axis of destination : 561 Pixel along y-axis of destination : 607 PASS! Dragged Element 'SALES' is Dropped at Target Location !!!


Refer next page Handling RadioButtons And CheckBoxes
bottom of page