How to Handle Actions Class in Selenium

Andreea Draniceanu

Posted On: June 12, 2024

view count147860 Views

Read time22 Min Read

Handling keyboard and mouse actions is essential for creating robust and comprehensive automated tests that mimic real user interactions. The Actions class in Selenium provides a suite of methods for simulating complex user interactions with web elements, such as clicking, double-clicking, right-clicking, dragging and dropping, and sending keyboard commands.

This functionality is crucial for web interactions beyond simple button clicks, such as filling out forms, navigating through menus, performing drag-and-drop operations, and more.

By leveraging the Actions class in Selenium, testers can automate a wide range of user behaviors, ensuring that the application responds correctly to various input scenarios, thus providing a thorough validation of the user interface.

What Is Actions Class in Selenium?

Actions in Selenium refer to a pivotal feature that empowers developers and testers to simulate complex user interactions within web applications. These interactions encompass a range of actions, such as mouse movements, keyboard inputs, drag-and-drop operations, and more.

Selenium’s Actions API plays a fundamental role in ensuring accurate and realistic automation of user behaviors, thereby enhancing the precision and effectiveness of testing processes.

In Selenium, the Actions class lets you create a sequence of actions, like mouse clicks or keyboard inputs, which can be executed using the perform() method. This means you can combine and execute different actions as a single unit.

For example, in the below scenario, you can see that to reach the Login button, you must first hover over the My account link, which expands the menu. Hovering over an element cannot be done using regular Selenium methods, so we need to use the Actions class to perform this action.

Methods of Actions Class in Selenium

It is quite easy to understand what the Actions class in Selenium is, the tricky part is implementing it. The actions that can be performed in a browser are broadly classified into two categories.

  • Mouse Actions
  • Keyboard Actions

Mouse Actions

Mouse actions in Selenium refer to the various interactions that can be simulated using a mouse, such as clicking, double-clicking, right-clicking, and dragging and dropping. These actions emulate a user’s interactions with a website, allowing for comprehensive testing of web applications.

Some of the commonly used mouse actions in Selenium are mentioned below:

  • click(): clicks at the current location.
  • doubleClick(): performs a double-click at the current mouse location.
  • contextClick(): performs a right-click at the current mouse location.
  • dragAndDrop(WebElement source, WebElement target): drags an element from the source location and drops in the target location.
  • moveToElement(WebElement target): moves to the target element.

Keyboard Actions

Keyboard actions in Selenium encompass the various interactions that can be performed using a keyboard, such as pressing, holding, and releasing keys. These actions mimic a user’s interactions with a website through the keyboard.

Some of the commonly used keyword actions in Selenium are mentioned below:

  • keyUp(WebElement target, java.lang.CharSequence key): performs a key release after focusing on the target element
  • keyDown(WebElement target, java.lang.CharSequence key): performs a key press after focusing on the target element
  • sendKeys(WebElement target, java.lang.CharSequence… keys): types the value.

Apart from the above methods, many other methods can be used based on your testing requirements. The extensibility of actions that can be performed is one of the best features of the Actions class in Selenium. These action methods are used similarly with various programming languages, such as C#, Python, Java, and more.

Info Note

Run your Selenium test script over 3000+ browsers and OS combinations. Try LambdaTest Today!

How To Handle Actions Class in Selenium Java?

Now that you understand the Actions class in Selenium, it is time to implement it in your test scripts.

Using Selenium with Java to handle the Actions Class is highly effective for testing complex user interactions in web applications. The Actions Class in Selenium allows for the simulation of advanced user actions such as drag-and-drop, hover, and keyboard events. Java’s powerful and versatile programming capabilities complement Selenium’s flexibility, enabling testers to create sophisticated and precise automated test scripts.

This combination ensures a thorough and accurate testing process, enhancing the reliability and robustness of your web application tests.

To implement the Actions class in Selenium with Java, follow the steps given below-

  1. First, import the package org.openqa.selenium.interactions.Actions.
  2. To use the methods provided by the Actions class, we need to create an object of this class and pass the WebDriver as an argument.
  3. The object created can now be used to perform any actions. You can see various actions this class provides once you create an object.
  4. Code example demonstrating the instantiation of WebDriver and Actions class in Selenium

Based on each method of the Actions class in Selenium, we will learn to implement each mouse and keyword action method below.

Mouse Actions

Let’s learn how to implement some mouse action methods, such as click(), contextClick(), and more, with detailed code examples.

click()

Actions class in Selenium can also perform a mouse click on a specific element or at the current mouse location.

Below is a simple code that is used to search for a product on a shopping website and click the search icon:

github

contextClick() & doubleClick()

If there is a requirement to click a button twice or right-click, the Actions class in Selenium can also do that. We can use the doubleClick() and contextClick() methods respectively.

Below is a simple code which can be used to perform both these actions:

github

moveToElement()

This method is used to move to a specific target element on the web page. Sometimes, some sub-options or sub-menus may be visible only when the mouse cursor is moved or hovered over the main menu.

In such cases, the Actions class in Selenium provides the moveToElement() method. We can also provide the x coordinate and the y coordinate as parameters in this method in addition to the target element.

Let us try to automate the scenario below.

Test Scenario:

  1. Navigate to the LambdaTest website.
  2. Hover over the Resources tab with your mouse.
  3. Click the Blog sub-menu.
  4. Read the articles posted in the blog.

Example of using the moveToElement() method in Selenium to hover over the Resources tab on the LambdaTest website and click the Blog sub-menu

github

dragAndDrop()

The dragAndDrop(WebElement source, WebElement target) method is used to drag an element from the source and drop it into the target location. There are two ways to perform this with the help of the Actions class in Selenium.

  • Use the dragAndDrop(WebElement source, WebElement target) method.
  • Use the given series of actions (clickAndHold(WebElement source) -> moveToElement(WebElement target) -> release)

github

Keyword Actions

Let’s learn how to implement some keyword action methods, such as sendKeys(), keyUp()/keyDown(), and more, with detailed code examples.

sendKeys()

We will use the sendKeys() method to send other keys like CTRL, ALT, SHIFT, etc.

While searching for some products on a shopping website, we would type the ‘Product name’ and press Enter from the keyboard. This action would be the same as that performed by clicking the Search button.

Below is the code for performing a search operation only using the keyboard Actions class in Selenium.

github

That is how you use the Actions class in Selenium with the sendKeys() method. You must be wondering- why we added build() and perform() at the end.

The build() method generates a composite action containing all actions ready to be performed. The perform() method performs the defined series of actions.

keyUp()/keyDown()

The keyUp() and keyDown() methods imitate the keyboard actions of pressing and releasing the keys. These methods include converting the texts into uppercase or lowercase, copying text from the source and paste in a destination location, scrolling up and down the web page, multiple value selections etc. This is one of the most repeatedly used Actions classes in Selenium test automation.

Let us see the sample code for each case-

  • Converting Texts to Uppercase
  • When we want to enter the text manually, we press down the SHIFT key and enter the text simultaneously without releasing the SHIFT key.

    We can apply the same logic to our code like below:

    github

    Result:

    Example of Selenium keyDown() and keyUp() methods in a test automation script converting text to uppercase

  • Scrolling Up & Down the page
  • You can also scroll to the top or the bottom of a page using the Actions class in Selenium test automation.

    github

    Note: I have intentionally added some wait time in the above code so that you can see how the page scrolls down and again to the top of the page.

  • Copying & Pasting
  • Copying some text from the source and pasting it into a target location can also be done with the help of the actions class in Selenium test automation. The logic is the same as how we copy and paste texts manually.

    Below is the sample code for copying the text and pasting it in another location:

    github

    Result:

    Screenshot demonstrating copying and pasting text using Selenium Actions class in test automation

  • Refreshing the page
  • Actions class in Selenium test automation can also perform basic refreshing operations. This might not seem as useful, but it does have its perks when a browser cannot read the contents of a page on the first try.

    Below is a sample code that can be used to refresh a web page:

github

Watch the following video below to learn the Actions Class in Selenium and how to use it.

Subscribe to the LambdaTest YouTube Channel for more video tutorials on automation testing, Selenium testing, Playwright testing, Cypress testing, and more.

How To Handle Actions Class in Selenium C#?

Using Selenium with C# to handle the Actions Class is an excellent choice for automating complex user interactions in web applications. The Actions Class in Selenium simulates advanced actions like drag-and-drop, hover, and multiple keyboard events. C#’s robust programming features and seamless integration with Selenium empower testers to craft precise and sophisticated test scripts.

This combination enhances the accuracy and reliability of your automated tests, ensuring comprehensive validation of your web application’s interactive functionalities.

To handle the Actions Class in Selenium using C# for mouse and keyboard actions, the following methods are commonly used:

Some of the commonly used mouse actions in Selenium C# are:

  • Click(): simulates a left mouse button click in the specified location.
  • ContextClick(): performs a right-click on the designated element.
  • DoubleClick(): simulates a left mouse button double-click in the specified location.
  • ClickAndHold(): simulates clicking and holding the left mouse button.
  • DragAndDrop(): drags and drops an element to a specified location.
  • MoveToElement(): moves the mouse cursor to a specified location.

Some of the commonly used keyword actions in Selenium C# are:

  • SendKeys(): simulates typing from the keyboard a given key
  • KeyUp(): releases a pressed key.
  • KeyDown(): presses and holds a key.

To implement these Action classes in Selenium using C#, you must first gather the necessary details to run your tests successfully. This includes identifying the elements you want to perform the actions, such as clicking, double-clicking, right-clicking, or dragging and dropping.

You also need to ensure that your WebDriver is properly configured and that the necessary drivers are installed for the browsers you intend to test against. Once you have gathered these details, you can start writing your tests using the Actions classes provided by Selenium.

Prerequisites

For demonstration purposes, we will use NUnit as the automation testing framework to run the tests. We will use the latest Selenium 4 version, 4.16.2, to write the test scripts. Additionally, we will add the Selenium WebDriver NuGet package to get started.

You can add the package by running the following package manager command:

NUnit and Selenium WebDriver installation command in package manager

The tests can run locally, on your local machine where the code resides, and remotely in the cloud. However, leveraging cloud cloud grid solutions offers distinct advantages.

Running tests in the cloud provides flexibility in choosing browsers and platforms and the ability to run tests in parallel, allowing you to scale the testing process. This can significantly reduce overall test execution time. This approach allows for efficient resource utilization and scalability, making managing and scaling test automation efforts easier.

One such cloud grid solution is LambdaTest, an AI-powered test orchestration and execution platform that lets you run manual, automation, and cross-browser testing at scale with over 3000+ real devices, browsers, and OS combinations.

Let’s execute mouse and keyboard action methods on the cloud platform. You’ll need your username and access key from the LambdaTest Profile > Accounts Settings > Password & Security tab.

Once you have copied and stored the username and access key, the next thing to do is to determine the capabilities to use in the test from the LambdaTest Capabilities Generator.

After adding the details and the test script in the ActionClass, this is what the test class looks like.

github

Code Walkthrough:

  1. Import the necessary Selenium libraries to help you interact with the web page.
    • OpenQA.Selenium, OpenQA.Selenium.Chrome (or the browser we want to use in our tests).
    • OpenQA.Selenium.Interactions, which gives us access to the Actions class.
    • OpenQA.Selenium.Remote for access to the Selenium Grid.

    Note: You can skip the last one to execute the tests locally.

    code-walkthrough-example-with-selenium-libraries-openqa-selenium-openqa-selenium-chrome-openqa-selenium-interactions-and-openqa-selenium-remote

  2. Within the class, you need to declare the variables. And if we’re using Selenium Grid, you will also need to declare the URL, username, and access key:
  3.  Declaring variables and Selenium Grid setup with URL, username, and access key

  4. The username and access key you collected earlier from the LambdaTest platform can be stored as environment variables because this is more secure than keeping them in the code as hard-coded values.
  5. To set environment variables, you need to use the following commands:

    • For Windows:
    • For macOS:
    • For Linux:
  6. Within the Setup(), we need to create a new instance of the WebDriver and add the driver capabilities.
  7. Code snippet showing WebDriver setup and driver capabilities configuration in Selenium

  8. The SetStatus() method is used in each test to set its status. It publishes the pass/fail status on the LambdaTest platform.
  9. LambdaTest platform status update image

The [SetUp] is a NUnit attribute, which defines a method executed before each test. So, this part of the code will run before each test.

If you want to keep your test local, it’s enough to create a new driver instance in the setup like this:

NUnit SetUp attribute

With the base file set up, including all the required instructions, we can now learn how to write the Actions class in Selenium C#, along with its syntax and code demonstration.

Mouse Actions

Below, we will learn how to implement mouse actions such as Click(), DoubleClick(), and more, in detail, with code demonstrations using the Actions Class in Selenium.

Click()

This action moves to the center of an element and presses and releases the left mouse button. This is a normal “clicking” action.

Syntax:

Let us understand the working of click() Action class in Selenium with a test scenario.

Test Scenario:

  1. Navigate to the URL: Selenium Playground.
  2. Locate the first checkbox element by its ID (found by right-clicking on it on the web page and selecting Inspect).
  3. Click the first checkbox.

To verify the checkbox status, you can use the SetStatus() method to capture and display the test status of the checkbox.

Below is the code for the above test scenario.

DoubleClick()

This action moves the mouse to the center of an element and performs a left mouse button double-click.

Syntax:

Let us understand the working of DoubleClick() Action class in Selenium with a test scenario.

Test Scenario:

  1. Navigate to the URL: Selenium Playground.
  2. Locate the first checkbox element.
  3. Double-click on the first checkbox.

Below is the code for the above test scenario.

ContextClick()

An action that moves the cursor to the center of an element and then presses the right mouse button. This can trigger various actions, such as opening new menus.

Syntax:

Let us understand the working of ContentClick() Action class in Selenium with a test scenario.

Test Scenario:

  • Navigate to the URL: Selenium Playground.
  • Locate the div element on the page.
  • Right-click on the div element.

Below is the code for the above test scenario.

ClickAndHold() & MoveByOffset()

ClickAndHold() and MoveByOffset() are essential methods in the Actions class using Selenium C#, enabling precise control over mouse interactions such as click-and-hold operations and moving the mouse to specific offsets.

  • MoveToElement(): This action moves the mouse cursor to the specified source element and then moves it by the number of pixels in the provided offset.
  • Syntax for MoveToElement():

    We must provide the pixels to move on the X and Y axes.

  • ClickAndHold(): This action moves the mouse to the center of a web element and then presses the left mouse button. It helps to focus on a specific element on the page.
  • Syntax for ClickAndHold():

Let us understand the workings of MoveToElement() and ClickAndHold() Action class in SeleniumC# with a test scenario.

Test Scenario:

  • Navigate to the URL: Selenium Playground.
  • Locate the first slider on the page.
  • Move the first slider by one-third.

The code below first identifies the web element and moves the slider to the desired location. It clicks and holds the mouse on the slider (using ClickAndHold()), moves it to the start (MoveByOffset()), then moves it to the desired location and releases the mouse button.

The ClickAndHold() action clicks the mouse at the element’s center. To move to the start of the slider, get the slider’s width, divide it by 2, and drag the mouse there using a negative offset to move left.

To move it by one-third of the total length, get the slider’s width and divide it by 3 or multiply it by 0.34 to move the mouse horizontally to the right using a positive offset.

DragAndDrop()

Using this action, you can drag and drop a web element from one point to another – either to another element or by offset.

Syntax:

The DragAndDrop() receives two parameters: the source element (which is to be dragged) and the target element (where the element will be dropped).

You can perform DragAndDropToOffset by providing 3 parameters: the element to be dragged and the number of pixels where the element needs to be moved on the X and Y axes (int values provide these).

Syntax:

Let us understand the working of the DragAndDrop() Action class in Selenium with a test scenario.

Test Scenario:

  1. Navigate to the URL: Selenium Playground.
  2. Locate the first draggable element.
  3. Locate the target element.
  4. Drag the first draggable element and drop it onto the target element.

Below is the code for the given test scenario.

MoveToElement()

This action moves the mouse pointer to the center of the given element without clicking on it. It’s useful when you need to hover an element.

Syntax:

Let us understand the working of the MoveToElement() Action Class in Selenium with a test scenario.

Test Scenario:

  1. Navigate to the URL: Selenium Playground.
  2. Locate the first element on the page.
  3. Hover over the first element.

Below is the code for the given test scenario.

Keyboard Actions

Below, we will learn how to implement keyboard actions such as SendKeys(), KeyUp(), and KeyDown() in detail, with code demonstrations using the Action Class in Selenium.

KeyUp()/KeyDown()

KeyUp() and KeyDown() are methods in the Selenium Action class that help simulate pressing and releasing keyboard keys, respectively. These methods are useful for handling keyboard interactions in automated tests.

  • KeyUp(): This action sends a modifier key-up message to the browser, which will release the pressed key. It should be used together with the key-down action.
  • Let’s say you want to type something in all uppercase letters by simulating the usage of the SHIFT key. This means you must press the SHIFT key, type the required text, and then release the pressed key.

    Below is code so you can understand the example given above.

    The same sequence can also be written as a single line of code:

  • KeyDown(): This action sends a modifier key down a message to the specified element in the browser, meaning it presses the provided key. This is useful in scenarios like copy-and-paste actions done through the keyboard shortcuts or performing a page refresh from the F5 key.

Syntax:

SendKeys()

The Actions API offers a useful method that integrates the key down and key up instructions into a single action. This command can be utilized when typing many characters while performing other tasks or using certain keyboard shortcuts, such as refreshing a page (F5).

For example, to perform a select-all action, you need to press down the Control key, type “a”, then release the Control key. This is done like this:

Syntax:

Let us understand the working of the SendKeys() Action Class in Selenium with a test scenario.

Test Scenario:

  • Navigate to the URL: Selenium Playground.
  • Enter some text in the input box using the sendKeys() method.
  • Press Shift+A to select the entire text.

Below is the code for the given test scenario.

Output:

To execute the tests, you can run them from Test Explorer or your pipeline if you have one set up. This demonstration uses Visual Studio.

The tests will be available in the Test Explorer panel.

Visual Studio Test Explorer panel

They currently have no status because they have not been executed. From the top buttons, you can run the current selection or press Run All. Tests can also be run from the command line.

When running on the grid, the test in progress is visible.

Screenshot of test execution buttons and grid interface

When running tests locally, a browser window will open for each test and close during the TearDown() process. After the tests finish executing, their status will be updated. If all tests pass, they will be marked as green.

Illustration of browser windows opening and closing during test execution

Result:

The results are also visible in the LambdaTest account under the Automation > Web Automation:

Screenshot of results displayed in LambdaTest Automation Dashboard under Web Automation section

You can see a video playback of the test as well:

To learn more about automation with the LambdaTest platform and its functionality, watch the video tutorial given below and get your automation testing process started.

Wrapping Up!!!

In summary, we’ve learned about the Actions class in Selenium and its methods for interacting with a browser. We’ve seen examples of actions like clicking the mouse, typing text, dragging and dropping, moving to an element, double-clicking, right-clicking, copying and pasting content, refreshing a page, and converting text cases.

To enhance your Selenium test automation, consider using a cloud Selenium Grid like LambdaTest to speed up your release cycles. LambdaTest offers a cloud-based Selenium Grid for cross-browser testing on over 3000+ OS and browsers. Implement these scripts effectively on LambdaTest and share your feedback. Also, share your experiences using the Actions class in Selenium with your friends and colleagues to expand our knowledge in this area.

Stay tuned for more interesting blogs on Selenium test automation. Happy Testing!

Frequently Asked Questions (FAQs)

Is Action a class or interface?

The Actions class is a class in Selenium that is used to handle complex user interactions.

What is the difference between the Robot and Actions classes in Selenium?

The Robot class simulates low-level keyboard and mouse events, like key presses and mouse clicks, outside the browser. The Actions class, on the other hand, is used for high-level interactions within the web browser, like drag-and-drop, mouse hover, and multiple action sequences.

What is the Page Action class in Selenium?

There is no specific Page Action class in Selenium. However, in the Page Object Model (POM) design pattern, an Actions class might be created to encapsulate methods for performing actions on a specific web page, enhancing code readability and maintainability.

Author Profile Author Profile Author Profile

Author’s Profile

Andreea Draniceanu

I’m a software QA engineer based in Romania. I’ve been in the software industry for over 10 years. My current focus is UI test automation with C#, but I love exploring all QA-related areas and sharing my knowledge.

Blogs: 13



linkedintwitter