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:
| Method | Description |
|---|---|
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();
