View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

What is Action Class in Selenium & It’s Applications?

By Rohan Vats

Updated on Nov 18, 2022 | 7 min read | 7.2k views

Share:

Action Class in Selenium is a built-in feature offered by selenium for managing keyboard and mouse events. It can manage multiple operations and events such as control key, drag and drop, and many more. The Action Class’s operations are implemented using the advanced user interaction API feature of the Selenium Webdriver. 

Check out our free courses to get an edge over the competition.

Commonly used Keyboard and Mouse Events

Special keyboard and mouse events are handled using the Advanced User Interactions API. This API contains the Action and the Actions class that are required while executing these events. Given below are the commonly used keyboard and mouse events provided in the Action class. 

Method Description
Click And Hold() To click without releasing at the current mouse location.
context-click() Performs a context-click(Right-click mouse action) at the current mouse location 
doubleClick() Performs a double-click action at the current mouse location
drag and drop(source, target)

Performs click-and-hold operation at the source element location, then moves to the target element location and releases the mouse.

Parameters:

source-element to emulate button-down events.

Target-element to move and release the mouse  

dragAnd DropBy(source, x-offset, y-offset) 

Performs click-and-hold at the source element location, moves by a specified offset and then releases the mouse.

Parameters:

source-element to emulate button-down events.

xOffset- to specify horizontal move offset.

yOffset- to specify vertical move offset.

keyDown(modifier_key)

Performs the modifier keypress event. Does not release the modifier key with subsequent interactions assuming that it is pressed.

Parameters:

modifier_key to represent any of the modifier keys, such as Keys.SHIFT, Keys.ALT, or Keys.CONTROL.

key Up(modifier_key)

Performs a key release.

Parameters:

modifier_key to represent any of the modifier keys, such as Keys.SHIFT, Keys.ALT or Keys.CONTROL.

moveByOffset(x-offset, y-offset)

Moves the mouse from the current position by the specified offset. 

Parameters:

x-offset to specify the horizontal offset. A negative value means the mouse is moving to the left.

y-offset to specify the vertical offset. A negative value means the mouse is moving downwards.

moveToElement(toElement)

Moves the mouse from the middle to the middle of the element.

Parameters:

element to represent the element to move to.

release() Releases the pressed mouse left mouse button at the current mouse location.
sendKeys(onElement, charsequence)

Sends a series of keystrokes on to the element.

Parameters:

Element to represent the element that will get the keystrokes, usually a text field.

Char sequence- any string value to represent the sequence of keystrokes that is sent. 

Examples of Application of the Selenium Action Class

1. Import Action and the Action class.

import org.open a.selenium.interactions.Action;

import org.open a.selenium.interactions.Actions;

Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)

2. Instantiate a new Actions object

Actions builder = new Actions(driver);

3. Instantiate an Action using the Action object in the previous step

Action mouseOverHome = builder

.moveToElement(link_Home)

.build();

Here, we are using the moveToElement() method to perform a mouse-over the Home link. The build() is always the final method to ensure that all actions are compiled in a single step.

Check out upGrad’s Java Bootcamp.  

4. Use the perform method while executing the Action object we declared in the previous step.

Syntax:

mouseOverHome.perform();

Example 2:

Let us take another example where the Action class is used to implement the textbox’s Autocomplete feature.

To do this manually, we press the shift key, type the text that must be entered in uppercase and then let go of the shift key. Put, Shift + alphabet keys are pressed together.

To mimic the same operation through automation script, the Actions Class method is utilized. 

1. Import-Package

Action and Actions class exist in the org.openqa.selenium.interactions package of the Web Driver API. To use these you must import packages:

org.openqa.selenium.interactions.Action;

org.openqa.selenium.interactions.Actions;

2. Instantiate the Action class

It would help if you created an object of the Actions class to invoke its methods. So, we instantiate the Actions class and use the WebDriver object to initiate the class. The syntax is:

Actions actions = new Actions (webdriver object);

3. Generate a sequence of actions

In this example, the sequence of steps to perform multiple actions are:

  • Press the Shift key
  • Send the desired text
  • Release the Shift key

To perform these actions, the Action class has methods such as:

  • keyDown method to press the Shift key.
  • sendKeys method to send the desired text.
  • The keyUp method releases the Shift key.
  • The keyDown acts as a modifier key press to focus on an element while the keyUp method releases a modifier key pressed. 

A modifier key changes the action of another key when both the keys are pressed simultaneously. E.g., Shift, Alt and Ctrl.

A sequence of actions is generated but these actions are performed on a webElement. The steps for this are:

WebElement element = driver.findElement;

actions.keyDown(element, Keys, Shift);

action.sendKeys(“TexttoBeConvertAndSendInUpperCase);

actions.keyUp(Keys.Shift);

The important thing to observe here is that if you happen to hover around any Action class method, an Actions class object will be returned

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months
View Program

Job-Linked Program

Bootcamp36 Weeks
View Program

4. Build the actions sequence

You can build the actions sequence through the build() method of the Actions class. This method generates the entire set of actions that are ready to be performed. The syntax is:

Action action = actions.build();

The build() method returns an object of type Action that represents a composite action built from a sequence of multiple actions invoked by method calls.

5. Perform action sequences

The last step in executing the Action class is performing the actions sequence through the perform() method of the Action interface. The syntax is:

action.perform();

The above-given steps must be executed to leverage all of the Actions class methods and perform different user actions using the keyboard and mouse.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

What Next?

If you’re interested to learn more about Full-Stack Software development, check out upGrad & IIIT-B’s Executive PG Program Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Frequently Asked Questions (FAQs)

1. What is action class in Selenium?

2. What are the uses of Selenium?

3. How to learn Selenium in Java?

Rohan Vats

Rohan Vats

408 articles published

Get Free Consultation

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

View Program
upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

View Program
upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

View Program