Saturday, September 14, 2019

Post#65.Drag and Drop (moveByOffset) in Selenium


Drag and Drop an Element to Location Specified By X, Y Coordinate Using Selenium
Drag and drop is now common functionality across many web applications for interactive user interface and easy upload. 

In this session you will come to know how To Drag and Drop an Element to Location Specified by X, Y Coordinate.

This can be achieved by using moveByOffset() method of Actions class

Scenario:
1. Launch https://jqueryui.com/draggable/

2. Drag and release the element an X=250 and Y= 10 Pixels.

i.e 250 pixel offset in horizontal direction and 10 Pixels in vertical direction

Java Code:

package pack5;
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 {

                               public static void main(String[] args) {

                               FirefoxDriver driver = new FirefoxDriver();
                               driver.get("https://jqueryui.com/draggable/");
                               driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
                               WebElement element = driver.findElement(By.id("draggable"));
                               Actions action = new Actions(driver);
                               action.clickAndHold(element).moveByOffset(250, 10).build().perform();
               }

}


No comments:

Post a Comment