Actions Class in Selenium WebDriver

HOME

import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

How to get an instance of Actions class?

We can create an instance of Actions class simply, by passing current webDriver instance. Now, using the action instance, we can call any of the methods provided in Actions class.

WebDriver driver = new ChromeDriver();
Actions builder = new Actions(driver);

How to see all the supported methods by Actions class?

In order to see all the supported methods by Actions class, press control + space after, Action object (builder)

There is a large number of methods available in Actions class. The below screenshot represents some of those methods

Methods in Actions class of Selenium

There are many methods in this class, which can categorized into four main categories:

Different Methods for performing Keyboard Events:

1) keyDown(java.lang.CharSequence key) – Performs a modifier key press 

2) keyUp(java.lang.CharSequence key) – Performs a modifier key release

3) sendKeys(java.lang.CharSequence… keys) – Sends keys to the active element.

Different Methods for performing Mouse Events:

1) click() – Clicks at the current mouse location.

2) clickAndHold() – Clicks (without releasing) in the middle of the given element.

3) contextClick() – Performs a context-click at middle of the given element.

4) doubleClick() – Performs a double-click at the current mouse location.

7) dragAndDrop(WebElement source, WebElement target) – A convenience method that performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.

8) dragAndDropBy(WebElement source, int xOffset, int yOffset) – A convenience method that performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.

9) moveByOffset(int xOffset, int yOffset) – Moves the mouse from its current position or (0,0) by the given offset.

10) MoveToElement(WebElement target) – Moves the mouse to the middle of the element.

Drag and Drop action in Selenium WebDriver

HOME

 actions.dragAndDrop(source,target).perform(); 

Let us see this with the help of an example.

import java.time.Duration;
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.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

public class dragAndDrop_Demo {
	
	protected static WebDriver driver;

	public static void main(String[] args) {
	  
       // Create a new instance of the Chrome driver
		ChromeOptions options = new ChromeOptions();
	    options.setImplicitWaitTimeout(Duration.ofSeconds(10));
	    options.addArguments("start-maximized");
	    driver = new ChromeDriver(options);

        // Launch the URL
	    driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html");

	    WebElement draggable = driver.findElement(By.id("draggable"));
        WebElement droppable = driver.findElement(By.id("droppable"));
        new Actions(driver)
                .dragAndDrop(draggable, droppable)
                .perform();

        System.out.println("Text appeared :" + driver.findElement(By.id("drop-status")).getText());
        driver.close();
	
    }   
}

Image before performing Drag and Drop operation

Image after performing Drag and Drop operation

Drag and Drop using clickAndHold, moveToElement and release Method


1. Hold the source
2. Move the element
3. Release the element

The different methods of Action class we will be using here for Drag and Drop in Selenium:
clickAndHold(WebElement element) – Clicks a web element at the middle(without releasing).
moveToElement(WebElement element) – Moves the mouse pointer to the middle of the web element without clicking.
release(WebElement element) – Releases the left click (which is in the pressed state).
build() – Generates a composite action

Let us understand the use of these methods in a Selenium program

import java.time.Duration;
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.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

public class clickAndHold_Demo {
	
	protected static WebDriver driver;

	public static void main(String[] args) {

		 // Create a new instance of the Chrome driver
		ChromeOptions options = new ChromeOptions();
	    options.setImplicitWaitTimeout(Duration.ofSeconds(10));
	    options.addArguments("start-maximized");
	    driver = new ChromeDriver(options);

        // Launch the URL
	    driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html");

	    WebElement clickable = driver.findElement(By.id("clickable"));
        new Actions(driver)
                .clickAndHold(clickable)
                .perform();
        
        System.out.println("Text appeared :" + driver.findElement(By.id("click-status")).getText());

	}

}

Drag and Drop using dragAndDropBy and offset method 

A convenience method that performs click-and-hold at the location of the source element, moves by a given offset, and then releases the mouse.
• Using the dragAndDropBy Method, we drop WebElement on a particular WebPage offset location.

Below is a Selenium program that shows the use of the dragAndDropBy method.

import java.time.Duration;
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.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

public class dragAndDropBy_Demo {

	private static WebDriver driver;
	
	public static void main(String[] args) {

		 // Create a new instance of the Chrome driver
		ChromeOptions options = new ChromeOptions();
	    options.setImplicitWaitTimeout(Duration.ofSeconds(30));
	    options.addArguments("start-maximized");
	    driver = new ChromeDriver(options);

        // Launch the URL
	    driver.get("https://www.selenium.dev/selenium/web/mouse_interaction.html");

	    WebElement draggable = driver.findElement(By.id("draggable"));
     
        new Actions(driver)
        .dragAndDropBy(draggable, 180,10)
        .perform();

        driver.close();
	}

}

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!