In this tutorial, we will discuss about Actions class in Selenium – What is Actions class, How to use, when to use and some commonly used methods of Actions class in Selenium.
In Selenium, we have Webdriver Element Commands to handle web page interactions like entering text in a text box using sendkeys(), refresh a page using refresh(), etc. Similarly to interact with dropdown, we use Select class with methods like selectbyindex, selectbyvalue, etc.
Suppose, we want to perform complex interactions or keyboard and mouse related interactions like right click, Drag and Drop an element, etc. We need advance API methods to implement these interactions, so Action class appears in place.
What is the Actions class in Selenium?
The user-facing API for emulating complex user gestures. Use this class rather than using the Keyboard or Mouse directly.
Implements the builder pattern: Builds a Composite Action containing all actions specified by the method calls.
Difference between Action and Actions class in Selenium
Actions is a class that based on a builder design pattern. This is a user-facing API for emulating complex user gestures.
Whereas Action is an Interface that represents a single user-interaction action. It contains one of the most widely used methods perform().
To use Action or Actions class, we need to import below mentioned packages:-
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
![](https://qaautomation.expert/wp-content/uploads/2021/02/image-186.png?w=400)
Methods in Actions class of Selenium
There are many methods in this class, which can categorized into four main categories:
Keyboard Actions
Mouse Events
Pen Actions
Scroll wheel Actions
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.
5) back Click – There is no convenience method for this, it is just pressing and releasing mouse button 3.
6) forward Click – pressing and releasing mouse button 4.
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.
Different Methods for performing Pen Events:
A Pen is a type of pointer input that has most of the same behavior as a mouse, but can also have event properties unique to a stylus. Additionally, while a mouse has 5 buttons, a pen has 3 equivalent button states:
- Using Pen
- Adding Pointer Event Attributes
Different Methods for performing Scroll Wheel Events:
1.Scroll to element – the actions class does not automatically scroll the target element into view, so this method will need to be used if elements are not already inside the viewport.
2. Scroll by given amount – Pass in an delta x and a delta y value for how much to scroll in the right and down directions. Negative values represent left and up, respectively.
3. Scroll from an element by a given amount – This scenario is effectively a combination of the above two methods.
4. Scroll from an element with an offset – This scenario is used when you need to scroll only a portion of the screen, and it is outside the viewport. Or is inside the viewport and the portion of the screen that must be scrolled is a known offset away from a specific element.
5. Scroll from a offset of origin (element) by given amount – The final scenario is used when you need to scroll only a portion of the screen, and it is already inside the viewport.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!