Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaHow to Perform Drag and Drop Offset Method in Selenium WebDriver using...

How to Perform Drag and Drop Offset Method in Selenium WebDriver using Java?

Selenium is an open-source web automation tool that supports many user actions to perform in the web browser. Automating a modern web page that has a drag and drop functionality and drag and drop is used to upload the files and so many user activities. so to perform the drag and drop actions the selenium provides a class called Actions. The Action class provides the method for drag and drop actions and many other mouse actions and Keyboard actions. The actions provided by this class are performed by an API called Advanced user interaction in the selenium web driver.

Let’s discuss performing drag and drop using the Action class in selenium web driver:

On some web pages, we need to perform the drag-and-drop functionality. In that case, we need to use the method that is provided by the actions class to perform this action.

There are two types of drag-and-drop methods provided by the Actions class:

  1. drag and drop using source and destination location.
  2. drag and drop using source and offset.

1. drag and drop using source and destination location

Actions action=new Actions(driver);

action.dragAndDrop(Source, Destination).perform();

2. drag and drop using source and offset

Actions action=new Actions(driver);

action.dragAndDropBy(Source, xoffset, yoffset).perform();

Here, this method gets three input parameters, one is for source location, and the other two for x and y offsets. This method is to click and hold the web element in the source location and then release the mouse in the specified location. The x offset is the horizontal movement and the y offset is the vertical movement.

Example Program:

In this example, we navigate to a URL and perform the drag-and-drop action using offset on that page.

Java




public class Geeks {
   
    public void neveropen() throws InterruptedException {
 
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        Thread.sleep(3000);
        Actions action=new Actions(driver);
        WebElement drag = driver.findElement(By.id("draggable"));
       
        action.dragAndDropBy(drag,150,50).build().perform();
        Thread.sleep(3000);
        driver.close();       
         
    }
}


In this program, we used the x and y offset as (150 and 50), and the method clicks and holds the draggable element and releases it in the specified offset location on the page. The output of this program is,

Output:

 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments