Actions Class in Selenium
What is the Actions class in Selenium?
org.openqa.selenium.interactions.Actions is Selenium's builder for complex mouse and keyboard interactions, including hover, right-click, double-click, drag-and-drop and key combinations.Actions is plural; Action represents a built sequence.perform(), or build and then perform the action.WebElement menu = driver.findElement(By.id("products"));
new Actions(driver)
.moveToElement(menu)
.click()
.perform();
perform(), using fixed sleeps, and mixing examples from different Selenium language bindings.Choose the right Selenium interaction
| Task | Recommended API | Why |
|---|---|---|
| Normal click or typing | WebElement.click() / sendKeys() | Simplest high-level interaction |
| Hover, right-click, double-click | Actions | Models a pointer sequence |
| Keyboard shortcut | keyDown(), sendKeys(), keyUp() | Keeps modifier state explicit |
| Wheel scrolling | Actions wheel input | Supported by Selenium 4's W3C Actions API |
Execution model: chained calls describe a sequence. perform() sends that sequence to the browser. Use build() when you need to store the resulting Action and run it later.
Frequently asked questions
What is the difference between Action and Actions?
Actions is the builder used to compose input steps. Action represents a built sequence that can be performed.
Why does moveToElement fail?
The element may be hidden, covered, stale or outside a ready layout. Wait for visibility and interactability, then locate it as close as possible to the action.
Does Actions support scrolling?
Yes. Selenium 4 includes wheel input actions, including scrolling by an amount, from an origin or to an element.
Primary reference: Selenium documentation — Actions API.
The Selenium Actions class builds composite keyboard, pointer and wheel input sequences. Use it for hover, double-click, right-click, drag-and-drop and keyboard shortcuts.
The Java class is org.openqa.selenium.interactions.Actions. The table below introduces common operations; verify exact signatures against the current Selenium Java API.
Action Class Table
| Method Name | Description |
|---|---|
| addAction | Adds an action to current list of actions to be performed. |
| build | Builds the sequence of actions. |
| click() | Clicks the mouse at the last known mouse coordinates. |
| click(IWebElement) | Clicks the mouse on the specified element. |
| clickAndHold() | Clicks and holds the mouse button at the last known mouse coordinates. |
| clickAndHold(IWebElement) | Clicks and holds the mouse button down on the specified element. |
| contextClick() | Right-clicks the mouse at the last known mouse coordinates. |
| contextClick(IWebElement) | Right-clicks the mouse on the specified element. |
| doubleClick() | Double-clicks the mouse at the last known mouse coordinates. |
| doubleClick(IWebElement) | Double-clicks the mouse on the specified element. |
| dragAndDrop | Performs a drag-and-drop operation from one element to another. |
| dragAndDropToOffset | Performs a drag-and-drop operation on one element to a specified offset. |
| equals | Determines whether the specified Object is equal to the current Object.(Inherited from Object.) |
| finalize | Allows an Object to attempt to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection.(Inherited from Object.) |
| getHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
| getLocatableFromElement | Gets the ILocatable instance of the specified IWebElement. Gets the Type of the current instance. |
| getType | (Inherited from Object.) |
| keyDown(String) | Sends a modifier key down message to the browser. |
| keyDown(IWebElement, String) | Sends a modifier key down message to the specified element in the browser. |
| keyUp(String) | Sends a modifier key up message to the browser. |
| keyUp(IWebElement, String) | Sends a modifier up down message to the specified element in the browser. |
| memberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
| moveByOffset | Moves the mouse to the specified offset of the last known mouse coordinates. |
| moveToElement(IWebElement) | Moves the mouse to the specified element. |
| moveToElement(WebElement, Int32, Int32) | Moves the mouse to the specified offset of the top-left corner of the specified element. |
| perform | Performs the currently built action. |
| release() | Releases the mouse button at the last known mouse coordinates. |
| release(IWebElement) | Releases the mouse button on the specified element. |
| sendKeys(String) | Sends a sequence of keystrokes to the browser. |
| sendKeys (IWebElement,String) | Sends a sequence of keystrokes to the specified element in the browser. |
| to String | Returns a String that represents the current Object. (Inherited from Object.) |
Steps To Perform
Step 1: Import the Actions and Action classes
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
Step 2: Instantiate a new Actions object.
Actions builder = new Actions(driver);Step 3: Instantiate an Action using the Actions object in step 2.
Action mouseOverHome = builder.moveToElement(link_Home).build();In this case, we are going to use the moveToElement() method because we are simply going to mouse-over the "Home" link. The build () method is always the final method used so that all the listed actions will be compiled into a single step.
Step 4: Use perform() method when executing the Action object we designed in Step 3.
mouseOverHome.perform();You can build a series of multiple actions using the Action and Actions classes. Just remember to close the series with the build () method.
Example
package com.javabykiran.automation;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class ActionsEx {
public static void main(String[] args) {
// opens a Firefox browser
WebDriver driver = new FirefoxDriver();
// opening of url driver.get("https://thekiranacademy.com/javabykiranfiles/");
Actions builder = new Actions(driver);
WebElement userName = driver.findElement(By.xpath(".//*[@ id='login_form_user']"));
Action seriesOfActions = builder.moveToElement(userName).click().keyDown(userName, Keys.SHIFT).
//press shift//for uppercase sendKeys(userName, "javabykiran"). // writes text doubleClick(userName).
// highlights test contextClick().// brings up menu
build();
seriesOfActions.perform();
}
}
