We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Master advanced user interactions beyond basic click and sendKeys commands
Throughout this unit, you've mastered Selenium's Action API, which enables you to simulate complex user interactions that go far beyond simple element.click() and element.sendKeys() commands.
Understanding when to use Actions vs basic commands and the builder pattern
keyDown, keyUp, sendKeys with modifier keys (Ctrl, Shift, Alt)
Standard clicks, double-clicks, click-and-hold, and context clicks
moveToElement for triggering hover effects, tooltips, and dropdown menus
dragAndDrop, dragAndDropBy, and manual drag sequences
scrollToElement, scrollByAmount, scrollFromOrigin (Chromium-only)
Stylus/pen pointer input with setActivePointer (Chromium-only)
Building complex sequences with the builder pattern and single perform()
Context clicks, pause between actions, multi-select patterns
The Actions class uses the builder pattern. Each method returns the Actions object, allowing you to chain multiple actions together. Only when you call perform() does Selenium execute the entire sequence.
Use Basic Commands When:
Use Actions When:
Cross-Platform Actions
Keyboard, mouse clicks, hover, drag-and-drop work on all browsers
Chromium-Only Actions
Scroll actions, pen actions only work on Chrome, Edge, and other Chromium-based browsers
actions.moveToElement(menuTrigger).perform();
WebElement menuItem = wait.until(
ExpectedConditions.visibilityOf(item)
);
actions.click(menuItem).perform();actions
.keyDown(Keys.CONTROL)
.sendKeys("a") // Ctrl+A
.keyUp(Keys.CONTROL)
.sendKeys(Keys.DELETE)
.perform();actions
.click(field1)
.sendKeys("value1")
.keyDown(Keys.TAB)
.keyUp(Keys.TAB)
.sendKeys("value2")
.keyDown(Keys.ENTER)
.keyUp(Keys.ENTER)
.perform();actions
.click(item1)
.keyDown(Keys.CONTROL)
.click(item2)
.click(item3)
.keyUp(Keys.CONTROL)
.perform();Here's a comprehensive example that demonstrates multiple Action API techniques in a realistic scenario:
1234567891011121314151617181920212223242526272829303132333435// Scenario: Fill a complex form with dropdown, drag-and-drop, and shortcuts
WebElement userName = driver.findElement(By.id("username"));
WebElement menuTrigger = driver.findElement(By.id("role-menu"));
WebElement roleOption = driver.findElement(By.linkText("Admin"));
WebElement draggable = driver.findElement(By.id("permission-drag"));
WebElement droppable = driver.findElement(By.id("permission-drop"));
WebElement submitBtn = driver.findElement(By.id("submit"));
Actions actions = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
// Step 1: Fill username with keyboard shortcut to clear first
actions
.click(userName)
.keyDown(Keys.CONTROL)
.sendKeys("a")
.keyUp(Keys.CONTROL)
.sendKeys(Keys.DELETE)
.sendKeys("john.doe@example.com")
.perform();
// Step 2: Hover over menu to reveal options, then click
actions.moveToElement(menuTrigger).perform();
wait.until(ExpectedConditions.visibilityOf(roleOption));
actions.click(roleOption).perform();
// Step 3: Drag-and-drop permission
actions.dragAndDrop(draggable, droppable).perform();
// Step 4: Scroll to submit button and click
actions.scrollToElement(submitBtn).perform();
actions.click(submitBtn).perform();
driver.quit();You've completed the Action API unit! You're now equipped to handle complex user interactions in your Selenium tests.
Apply these techniques to your own web applications. Start with simple actions and gradually build complex sequences.
Master combining Actions with explicit waits for robust, reliable tests that handle timing issues gracefully.
Test your applications with different browsers, screen sizes, and interaction patterns to ensure broad compatibility.