Careeroza
Actions Class Basics
Building a Chain
Some user actions are not a simple click: hovering, holding a key, dragging. The Actions class builds a chain of low-level mouse and keyboard steps and sends them with perform().
import org.openqa.selenium.interactions.Actions;
Actions actions = new Actions(driver);
actions.moveToElement(menu) // step 1
.pause(Duration.ofMillis(300))
.click(subItem) // step 2
.perform(); // nothing happens until perform()Note: Forgetting perform() is a classic mistake: the code runs without error and nothing happens in the browser.
Common Methods
| Method | Action |
|---|---|
moveToElement(el) | hover |
click(), doubleClick(el), contextClick(el) | click, double click, right click |
clickAndHold(el), release(el), dragAndDrop(src, target) | drag |
keyDown(Keys.SHIFT), keyUp(...), sendKeys(...) | keyboard |
scrollToElement(el), scrollByAmount(0, 600) | scroll (Selenium 4.2+) |