</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

What is the Actions class in Selenium WebDriver?

Actions class in Selenium provides methods to perform complex keyboard and mouse interactions like hover, drag-drop, and right-click.

Answer

The Actions class (org.openqa.selenium.interactions.Actions) provides an API to perform complex user interaction sequences — mouse movements, keyboard events, drag-and-drop, etc.

Import and setup:

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

Actions actions = new Actions(driver);

Common Actions methods:

MethodDescription
moveToElement(el)Mouse hover over element
click(el)Click an element
doubleClick(el)Double click
contextClick(el)Right click
dragAndDrop(src, tgt)Drag from source to target
sendKeys(keys)Send keyboard keys
clickAndHold(el)Click without releasing
release(el)Release mouse button
keyDown(Keys.CONTROL)Hold a key
keyUp(Keys.CONTROL)Release a key

Key pattern — always end with build().perform():

Java
actions.moveToElement(menuItem)
       .click(subMenuItem)
       .build()
       .perform();

Follow AutomateQA

Related Topics