Action Class in Selenium

โšก Smart Summary

Action Class in Selenium handles advanced keyboard and mouse events such as hover, drag-and-drop, right-click, and key combinations through the Advanced User Interactions API. This tutorial explains the Actions and Action classes, the key methods, and how to chain multiple interactions into one sequence.

  • ๐Ÿ–ฑ๏ธ Core Purpose: The Actions class automates compound mouse and keyboard events the basic click() cannot.
  • ๐Ÿ”‘ Two Classes: Actions builds the interaction; Action stores the compiled sequence to execute.
  • ๐Ÿงฑ Build then Perform: Chain methods, finish with build(), and run with perform().
  • ๐Ÿ“‹ Key Methods: moveToElement(), dragAndDrop(), keyDown()/keyUp(), contextClick(), and sendKeys().
  • ๐Ÿค– AI Enablement: AI assistants generate Action class code and self-heal locators during interaction tests.

Mouse-over output

Action Class in Selenium

Action Class in Selenium is a built-in feature for handling keyboard and mouse events. It supports operations such as multiple clicks with the Control key, drag-and-drop, and many more. These operations are performed using the Advanced User Interactions API in Selenium WebDriver.

Handling Keyboard & Mouse Events

Handling special keyboard and mouse events is done using the Advanced User Interactions API. It contains the Actions and Action classes that are needed to execute these events. The most commonly used keyboard and mouse events provided by the Actions class are listed below.

Method Description
clickAndHold() Clicks (without releasing) at the current mouse location.
contextClick() Performs a context-click (right click) at the current mouse location.
doubleClick() Performs a double-click at the current mouse location.
dragAndDrop(source, target) Click-and-holds at the source element, moves to the target element, then releases the mouse. Parameters: source (element to press on), target (element to release at).
dragAndDropBy(source, x-offset, y-offset) Click-and-holds at the source element, moves by a given offset, then releases. Parameters: source, xOffset (horizontal), yOffset (vertical).
keyDown(modifier_key) Presses a modifier key without releasing it, so later interactions assume it stays pressed. Parameter: a modifier key (Keys.ALT, Keys.SHIFT, or Keys.CONTROL).
keyUp(modifier_key) Releases a modifier key. Parameter: a modifier key (Keys.ALT, Keys.SHIFT, or Keys.CONTROL).
moveByOffset(x-offset, y-offset) Moves the mouse from its current position (or 0,0) by the given offset. A negative x moves left; a negative y moves up.
moveToElement(toElement) Moves the mouse to the middle of the element. Parameter: the element to move to.
release() Releases the depressed left mouse button at the current location.
sendKeys(onElement, charsequence) Sends a series of keystrokes to an element. Parameters: onElement (usually a text field), charsequence (the keystrokes to send).

In the following example, we use the moveToElement() method to hover over one of the Mercury Tours table rows.

Handling Keyboard and Mouse Events

The cell shown above is part of a <TR> element. When not hovered, its color is #FFC455 (orange). After hovering, the cell’s color becomes transparent and matches the background of the surrounding table.

Step 1) Import the Actions and Action classes.

Import Actions and Action classes

Step 2) Instantiate a new Actions object.

Instantiate Actions object

Step 3) Instantiate an Action using the Actions object from Step 2.

Instantiate Action object

Here we use the moveToElement() method to hover over the “Home” link. The build() method is always called last so that all listed actions are compiled into a single step.

Step 4) Use the perform() method to execute the Action object designed in Step 3.

Perform the action

Below is the full WebDriver code that checks the background color of the <TR> element before and after the mouse-over.

package newproject;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class PG7 {

    public static void main(String[] args) {
        String baseUrl = "https://demo.guru99.com/test/newtours/";
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();

        driver.get(baseUrl);
        WebElement link_Home = driver.findElement(By.linkText("Home"));
        WebElement td_Home = driver.findElement(By.xpath(
            "//html/body/div/table/tbody/tr/td"
            + "/table/tbody/tr/td"
            + "/table/tbody/tr/td"
            + "/table/tbody/tr"));

        Actions builder = new Actions(driver);
        Action mouseOverHome = builder.moveToElement(link_Home).build();

        String bgColor = td_Home.getCssValue("background-color");
        System.out.println("Before hover: " + bgColor);
        mouseOverHome.perform();
        bgColor = td_Home.getCssValue("background-color");
        System.out.println("After hover: " + bgColor);
        driver.close();
    }
}

The output below shows that the background color became transparent after the mouse-over.

Mouse-over output

Building a Series of Multiple Actions

You can build a series of actions using the Action and Actions classes. Remember to close the series with the build() method. Consider the sample code below.

Building a series of multiple actions

public static void main(String[] args) {
    String baseUrl = "https://www.facebook.com/";
    WebDriver driver = new FirefoxDriver();

    driver.get(baseUrl);
    WebElement txtUsername = driver.findElement(By.id("email"));

    Actions builder = new Actions(driver);
    Action seriesOfActions = builder
        .moveToElement(txtUsername)
        .click()
        .keyDown(txtUsername, Keys.SHIFT)
        .sendKeys(txtUsername, "hello")
        .keyUp(txtUsername, Keys.SHIFT)
        .doubleClick(txtUsername)
        .contextClick()
        .build();

    seriesOfActions.perform();
}

Series of actions output

FAQs

The Action class represents a compiled sequence of keyboard and mouse interactions built with the Actions class. It is executed through the Advanced User Interactions API in Selenium WebDriver.

Actions (plural) is the builder used to chain interactions like hover and click. Action (singular) is the compiled object returned by build() that you run with perform().

build() compiles all the chained interactions into a single composite Action. It is always called last, just before perform(), so the queued steps execute together as one sequence.

perform() executes the built Action against the browser. Without it, the chained interactions are only queued and never actually sent to the page.

Use moveToElement(element) on an Actions object, then call build() and perform(). This moves the cursor to the element’s center and triggers any hover behavior, such as a dropdown menu.

It supports clickAndHold, contextClick, doubleClick, dragAndDrop, dragAndDropBy, keyDown, keyUp, moveByOffset, moveToElement, release, and sendKeys โ€” covering most advanced interactions.

AI tools use self-healing locators that adapt when elements change, reducing flaky failures in hover and drag-and-drop tests. They also recommend stable XPaths and add smart waits automatically.

Yes. AI code assistants turn a plain-English scenario such as “hover over Home then right-click” into a working Actions chain with build() and perform(), and suggest robust locators.

Summarize this post with: