Drag And Drop Using Actions Class In Selenium WebDriver
Drag And Drop Using Actions Class:
In some applications, we may face a situation to automate drag and drop an item from one location to another location. We could not achieve these using basic elements. Selenium has provided an “Actions” class to handle this kind of scenarios. We overcome this kind of scenarios such as drag and drop using Actions Class.
To achieve this we use Actions class in Selenium WebDriver. You could find a detailed explanation on Actions in Selenium using below link.
Must Read: Actions Class in Selenium WebDriver
//To get source locator WebElement sourceLocator = driver.findElement(By.xpath("xpath")); //To get target locator WebElement targetLocator = driver.findElement(By.xpath("xpath")); //create object 'action' of Actions class Actions action = new Actions(driver); //use dragAndDrop() method. It accepts two parametes source and target. action.dragAndDrop(sourceLocator, targetLocator).build().perform();
Sample Script:
Given clear explanation in the comments section within the program itself. Please go through it to understand the flow.
package softwareTestingMaterial; 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; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; public class ActionsClass { @Test public void actionsClass() throws InterruptedException{ System.setProperty("webdriver.chrome.driver", "D:\\Selenium Environment\\Drivers\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //Create an object 'action' Actions action = new Actions(driver); //navigate to the required url where we could do drag and drop action driver.get("http://jqueryui.com/droppable/"); //WebdriverWait is used to wait for a frame to be available. Once it is availble we switch to the frame to achieve our task WebDriverWait wait = new WebDriverWait(driver, 5); wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector(".demo-frame"))); //To get source locator WebElement sourceLocator = driver.findElement(By.cssSelector("#draggable")); //To get target locator WebElement targetLocator = driver.findElement(By.cssSelector("#droppable")); //dragAndDrop(source, target) method accepts two parameters source and locator. //used dragAndDrop method to drag and drop the source locator to target locator action.dragAndDrop(sourceLocator, targetLocator).build().perform(); } }
If you are not a regular reader of my blog then I highly recommend you to signup for the free email newsletter using the below link.
Hi ………HappyLearning,
please let me know whats the use of build() method in below code.
action.dragAndDrop(sourceLocator, targetLocator).build().perform();
Hi Neevetha,
build().perform() method is used to perform sequence of operations where as just perform() method is used with out build().perform() when you are planning to do only single action.
Thanks