Actions Class in Selenium WebDriver

In this article, we will learn about the Actions class and its uses in Selenium.

While interacting with web applications we can easily perform actions like setting a value in a text box, click a button and select a value from a dropdown and etc using web driver commands. Having said there are few actions that we can’t simply perform using web driver commands like interacting with the browser through a mouse or keyboard. Some of such actions are mentioned as follows.

  • Double click an element
  • Right-click on an element
  • Drag and Drop and element
  • Moving mouse at the desired offset position
  • Keyboard press and release events
  • Selecting multiple elements using control key and etc

In order to deal with the above actions, we will have to use Actions Class in Selenium.

What is Actions Class in Selenium?

Actions Class in Selenium is loaded with features to handle keyboard and mouse events that cater to various types of actions as mentioned above and many more using the advanced user interactions API in Selenium Webdriver.

Handle Keyboard And Mouse Events Using Actions Class In Selenium

The operations that can be performed in an application are comprehensively characterized into two different categories.

  1. Mouse actions
  2. Keyboard actions

Mouse Actions in Selenium

Selenium does have many methods to perform mouse action but we will discuss only a few of the most widely used methods that suffice the needs.

  • click() – Clicks at the current mouse location
  • doubleClick(): Performs double click on the element
  • contextClick(): Performs right-click on the mouse
  • dragAndDrop(WebElement source, WebElement target): Drags the element from one source location and drops to the target location.
  • moveToElement(WebElement target): Moves the mouse to the center of the element
  • clickAndHold(): Performs long click in the middle of the element

Keyboard Actions in Selenium

The following are keyboard actions provided by the Actions class

  • sendKeys​(java.lang.CharSequence… keys): Sends keys to the active element.
  • sendKeys(WebElement target, java.lang.CharSequence… keys) : Send keys to the provided element.
  • keyUp(WebElement target, java.lang.CharSequence key): performs a key release after focusing on an element
  • keyDown(WebElement target, java.lang.CharSequence key):performs a keypress after focusing on an element

Note: If you are using more than one action method together you will have to use build() method in conjunction with the respective actions. And eventually, you will have to use the perform() method to perform the currently built action. If you are getting confused you do not need to worry about at this point. By looking at the following examples things will be get clarified.

How to Define Actions Class in Selenium WebDriver

We can define an action class using the following syntax.

Examples of Using Actions Class in Selenium

How to Move the Mouse to an Element and click on it?

In the following example, we will navigate to W3School and perform the following actions.

  • Get the background color of the TUTORIALS menu link 
  • Hover the mouse or Move the mouse on the TUTORIALS menu link and get its changed background color.

Action class mouse movement in Selenium

  • Click on the TUTORIALS menu link.

Output

Background Color Before Mouse hover: rgba(76, 175, 80, 1)
Background Color After Mouse hover: rgba(76, 175, 80, 1)

Opening a New Tab By Performing Multiple Actions Together

We will open the same TUTORIALS menu link in a new tab by using keyDown,click and keyUp methods and building them in a sequence.

Conclusion

We can emulate keyboard and mouse events using the Actions class.

Note: We can also perform actions on any element by moving the mouse on the xand coordinates.

Recommended Posts

 

Leave a Reply