Skip to main content

Actions Class

Introduction​

The Actions class in Selenium 4 provides advanced user interaction capabilities for both mouse and keyboard gestures. It models real user behavior using the W3C input action specification.

Use Actions when:

  • Hover is required
  • Drag-and-drop is needed
  • Complex keyboard combinations are required
  • Standard click() fails

Selenium 4 fully aligns with the W3C WebDriver standard, making Actions more stable and predictable than earlier versions.


Basic Setup​

import org.openqa.selenium.interactions.Actions;

Actions actions = new Actions(driver);

Execute using:

actions.perform();

build().perform() is optional in Selenium 4 and mainly useful for complex multi-step chains.


πŸ–± Mouse Interactions

1. Simple Click​

actions.click(element).perform();

Use when normal element.click() fails due to overlays or JS listeners.


2. Click and Hold​

actions.clickAndHold(element).perform();

Often used for drag operations.


3. Release​

actions.release().perform();

Typically chained after clickAndHold().


4. Double Click​

actions.doubleClick(element).perform();

5. Right Click (Context Click)​

actions.contextClick(element).perform();

6. Hover (Mouse Over)​

actions.moveToElement(element).perform();

Required for menus, tooltips, hidden controls.


7. Move by Offset​

actions.moveByOffset(50, 100).perform();

Moves relative to current pointer position.


8. Drag and Drop​

Standard​

actions.dragAndDrop(source, target).perform();

By Offset​

actions.dragAndDropBy(source, 100, 0).perform();

Custom Reliable Pattern​

actions.clickAndHold(source)
.moveToElement(target)
.release()
.perform();

⌨ Keyboard Interactions (Frequently Missed Concepts)

Keyboard handling is a major part of the Actions API and is often underused.


1. sendKeys via Actions​

actions.sendKeys("Hello World").perform();

Useful when no element is directly focused.


2. keyDown() and keyUp() πŸ”₯ IMPORTANT​

Used for modifier keys like:

  • SHIFT
  • CONTROL
  • ALT
  • COMMAND

Example: Typing Uppercase​

actions.keyDown(Keys.SHIFT)
.sendKeys("selenium")
.keyUp(Keys.SHIFT)
.perform();

3. Copy-Paste Example (CTRL + A, CTRL + C, CTRL + V)​

actions.keyDown(Keys.CONTROL)
.sendKeys("a")
.sendKeys("c")
.keyUp(Keys.CONTROL)
.perform();

Then paste:

actions.keyDown(Keys.CONTROL)
.sendKeys("v")
.keyUp(Keys.CONTROL)
.perform();

4. Sending Keys to Specific Element​

actions.sendKeys(element, "Text Input").perform();

5. Multiple Modifier Keys​

Example: CTRL + SHIFT + T

actions.keyDown(Keys.CONTROL)
.keyDown(Keys.SHIFT)
.sendKeys("t")
.keyUp(Keys.SHIFT)
.keyUp(Keys.CONTROL)
.perform();

6. Using pause() Between Actions​

actions.clickAndHold(source)
.pause(Duration.ofSeconds(1))
.moveToElement(target)
.release()
.perform();

Useful for slow drag animations.


πŸ”„ Composite Actions (Chaining)

Actions allows combining mouse + keyboard:

actions.moveToElement(element)
.click()
.keyDown(Keys.SHIFT)
.sendKeys("abc")
.keyUp(Keys.SHIFT)
.perform();

This simulates real human behavior.


🧠 Scroll & Wheel Actions (Selenium 4)

Scroll to Element​

actions.scrollToElement(element).perform();

Scroll by Amount​

actions.scrollByAmount(0, 300).perform();

Preferred over JavaScript scroll in modern Selenium.


πŸ”¬ Low-Level Pointer Control (Advanced)

Using PointerInput and Sequence for:

  • Custom coordinates
  • Back/Forward mouse buttons
  • Fine-grained event timing

Only use in advanced scenarios. Wrap inside utilities.


Actions vs WebElement Methods

ScenarioRecommended Approach
Simple clickelement.click()
Hover neededActions.moveToElement()
Drag and dropActions.dragAndDrop()
Modifier keys requiredkeyDown() / keyUp()
Complex gestureChained Actions

Common Mistakes ❌

  • Forgetting .perform()
  • Not releasing modifier keys
  • Mixing JS click + Actions randomly
  • Using Actions when simple click works
  • Not waiting before performing actions

Best Practices βœ…

  • Always apply explicit waits before Actions
  • Keep complex chains inside page methods
  • Prefer high-level APIs first
  • Release modifier keys properly
  • Use pause() only when necessary

Interview Notes 🎯

Q: What is keyDown used for? To press modifier keys like SHIFT or CONTROL before sending other keys.

Q: Difference between sendKeys() and Actions sendKeys()? WebElement sendKeys targets a specific element. Actions can simulate global keyboard behavior.

Q: Why use Actions instead of click()? For hover, drag, modifier keys, and complex gestures.


Summary

  • Actions handles advanced mouse & keyboard gestures
  • keyDown/keyUp are critical for modifier combinations
  • Selenium 4 provides stable W3C-compliant input actions
  • Use Actions intentionally, not everywhere

Summary Table – Common Actions Methods​

Use CaseRecommended Method Chain (Java)
Simple click (real pointer click)actions.click(element).perform();
Hover (mouse over)actions.moveToElement(element).perform();
Double clickactions.doubleClick(element).perform();
Right click (context menu)actions.contextClick(element).perform();
Click and holdactions.clickAndHold(element).perform();
Click–hold β†’ move β†’ releaseactions.clickAndHold(src).moveToElement(dst).release().perform();
Drag and drop (element to element)actions.dragAndDrop(src, dst).perform();
Drag by offset (sliders)actions.dragAndDropBy(src, x, y).perform();
Move by offsetactions.moveByOffset(x, y).perform();
Scroll to element (Selenium 4)actions.scrollToElement(element).perform();
Scroll by amountactions.scrollByAmount(0, 300).perform();
Send keys globallyactions.sendKeys("text").perform();
Send keys to specific elementactions.sendKeys(element, "text").perform();
Hold modifier key (SHIFT example)actions.keyDown(Keys.SHIFT).sendKeys("abc").keyUp(Keys.SHIFT).perform();
CTRL + A (select all)actions.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTOL).perform();
Multiple modifier keysactions.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys("t").keyUp(Keys.SHIFT).keyUp(Keys.CONTROL).perform();
Add delay between actionsactions.clickAndHold(src).pause(Duration.ofSeconds(1)).release().perform();
Mouse + keyboard comboactions.moveToElement(el).click().keyDown(Keys.SHIFT).sendKeys("abc").keyUp(Keys.SHIFT).perform();