"Automation is not about replacing jobs; it's about empowering people to do more meaningful work." — Unknown

3. Actions Class

Estimated reading: 2 minutes 27 views

The Actions Class in Selenium is used to perform complex user interactions, such as mouse movements, clicks, key presses, drag and drop, and keyboard actions. It is a powerful class that simulates real user interactions with the browser, which cannot be accomplished with basic WebDriver commands alone.

Common Actions You Can Perform Using the Actions Class:

  1. Mouse Actions: Hovering, clicking, double-clicking, right-clicking, dragging and dropping, etc.
  2. Keyboard Actions: Pressing keys, releasing keys, sending key sequences, etc.
  3. Combination of Mouse and Keyboard: Actions like Ctrl + A, Shift + Click, etc.

Summary of Key Methods with Sample Syntax

 

Method NameDescriptionSample Syntax
click()Clicks on an element.actions.click(element).perform();
clickAndHold()Clicks and holds an element.actions.clickAndHold(element).perform();
contextClick()Right-clicks on an element.actions.contextClick(element).perform();
dragAndDrop()Drags one element and drops it on another element.actions.dragAndDrop(source, target).perform();
doubleClick()Double-clicks on an element.actions.doubleClick(element).perform();
keyDown()Holds down a modifier key (Ctrl, Shift, etc.).actions.keyDown(Keys.SHIFT).perform();
keyUp()Releases a modifier key (Ctrl, Shift, etc.).actions.keyUp(Keys.SHIFT).perform();
moveByOffset()Moves the mouse by a certain offset.actions.moveByOffset(10, 20).perform();
moveToElement()Moves the mouse to an element.actions.moveToElement(element).perform();
pause()Pauses the action for a specified time (in milliseconds).actions.pause(1000).perform();
release()Releases the mouse after a click-and-hold action.actions.release().perform();
sendKeys()Sends keyboard input to an element.actions.sendKeys(Keys.ENTER).perform();

Leave a Comment

Share this Doc

3. Actions Class

Or copy link

CONTENTS