Actions Class in Selenium WebDriver

HOME

import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

How to get an instance of Actions class?

We can create an instance of Actions class simply, by passing current webDriver instance. Now, using the action instance, we can call any of the methods provided in Actions class.

WebDriver driver = new ChromeDriver();
Actions builder = new Actions(driver);

How to see all the supported methods by Actions class?

In order to see all the supported methods by Actions class, press control + space after, Action object (builder)

There is a large number of methods available in Actions class. The below screenshot represents some of those methods

Methods in Actions class of Selenium

There are many methods in this class, which can categorized into four main categories:

Different Methods for performing Keyboard Events:

1) keyDown(java.lang.CharSequence key) – Performs a modifier key press 

2) keyUp(java.lang.CharSequence key) – Performs a modifier key release

3) sendKeys(java.lang.CharSequence… keys) – Sends keys to the active element.

Different Methods for performing Mouse Events:

1) click() – Clicks at the current mouse location.

2) clickAndHold() – Clicks (without releasing) in the middle of the given element.

3) contextClick() – Performs a context-click at middle of the given element.

4) doubleClick() – Performs a double-click at the current mouse location.

7) dragAndDrop(WebElement source, WebElement target) – A convenience method that performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.

8) dragAndDropBy(WebElement source, int xOffset, int yOffset) – A convenience method that performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.

9) moveByOffset(int xOffset, int yOffset) – Moves the mouse from its current position or (0,0) by the given offset.

10) MoveToElement(WebElement target) – Moves the mouse to the middle of the element.

Leave a comment