---
title: How to use the Actions Class In Selenium | Testing Resources
description: Selenium WebDriver comes with an Action Class, which allows you to simulate
  user input events, such as mouse and keyboard actions.
source_url:
  html: https://testingbot.com/resources/articles/what-are-selenium-actions
  md: https://testingbot.com/resources/articles/what-are-selenium-actions.md
---

![How to use the Actions Class In Selenium](https://testingbot.com/assets/resources/articles/8.webp)

# How to handle Action class in Selenium

Selenium WebDriver comes with an Action Class, which allows you to simulate user input events, such as mouse and keyboard actions.

By [Jochen D.](https://testingbot.com/about/jochen-d)2021-08-25

 Share on Facebook 

 Share on Twitter 

 Post on Reddit 

 Share link 

Selenium WebDriver comes with an Action Class, which allows you to simulate user input events, such as mouse and keyboard actions.

This article will give you an overview of what Selenium Actions can do, and how to use them in your tests.

## Table of Contents

- [What are Selenium Actions?](https://testingbot.com#introduction)
- [How does the Selenium Action Class work?](https://testingbot.com#class)
- [How to perform mouse actions with Selenium WebDriver?](https://testingbot.com#click)
- [How to perform keyboard actions with Selenium?](https://testingbot.com#keyboard)
- [How to combine multiple actions with the Selenium Actions class?](https://testingbot.com#multiple)

## What are Selenium Actions?

Selenium Actions allow you to perform UI actions in your test.   
 Clicking, double-clicking, hovering or other complex mouse actions can be scripted with an action.

Advanced user interactions such as holding a key while clicking something, or dragging and dropping an item are supported with Selenium Actions.

These actions are performed by the Advanced User Interactions API, which consists of the Selenium Action class to perform these interactions.

## How does the Selenium Action Class work?

The Selenium Action class is able to perform 2 types of user input: mouse actions and keyboard actions.

### Mouse Actions

The mouse actions will mimic various actions that a user can do on your website:

- `click()`: clicks at the current mouse location
- `doubleClick()`: double click at the current mouse location
- `contextClick()`: right click at the current mouse location
- `dragAndDrop(WebElement source, WebElement target)`: drags an element from one location to the other
- `moveToElement(WebElement target)`: moves an element 

### Keyboard Actions

The keyboard actions will mimic various keyboard actions that a user can do on your website:

- `keyUp(WebElement target, java.lang.CharSequence key)`: does a key release after focusing on the target element
- `keyDown(WebElement target, java.lang.CharSequence key)`: does a key press after focusing on the target element
- `sendKeys(WebElement target, java.lang.CharSequence... keys)`: types a sequence of keys.

There are more methods available in the Action class, please see this [overview of Actions class](https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html) documentation page.

To use the Selenium Actions class in your Java test project, please import the necessary class:

```java
import org.openqa.selenium.interactions.Actions
```

## How to perform mouse actions with Selenium WebDriver?

You can perform several mouse interactions with the Selenium Action class, including: hovering, clicking and double-clicking.

Please see the example below where we'll perform several mouse actions in our Java test case.   
 We'll find an element through Xpath, hover over the element and finally double click it.

```java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Mouse {
   public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();

       	driver.get("https://testingbot.com/");

        Actions ac = new Actions(driver);

        // Find the element we want to interact with
		WebElement freeTrialAction = driver.findElement(By.xpath("/html/body/div[2]/nav/div/div[3]/a[1]"));

        // Hover over the element
        ac.moveToElement(element).build().perform();
        // Double click on element
		a.doubleClick(freeTrialAction).perform();

		driver.quit();  
   }
}
```

## How to perform keyboard actions with Selenium?

Selenium Actions allow you to perform keyboard operations such as scrolling up and down the page, copy and pasting (via keyboard)

In the example below we'll show how to do various actions with the keyboard during your test.

```java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Mouse {
   public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();

       	driver.get("https://testingbot.com/");

        Actions ac = new Actions(driver);

        // Find the element we want to interact with
		WebElement freeTrialAction = driver.findElement(By.xpath("/input"));

        // Type something with SHIFT pressed down (uppercase)
        ac.keyDown(element, Keys.SHIFT).sendKeys("testingbot").build().perform();
        // Scroll Down using the CTRL+END keys
		ac.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
		// Paste the clipboard contents - depending on the OS under test, this may use different keys
		ac.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).build().perform();

		driver.quit();  
   }
}
```

## How to combine multiple actions with the Selenium Actions class?

Because Selenium Actions uses the builder pattern, it's possible to chain multiple actions together.   
 This means you can create complexer user input actions, a series of actions.

Please see the example below where the test will press the SHIFT key, type something, release the shift key, double-click an element and right-click - all in one action.

```java
Actions builder = new Actions(driver);
Action seriesOfActions = builder
	.moveToElement(textField)
	.click()
	.keyDown(txtUsername, Keys.SHIFT)
	.sendKeys(txtUsername, "testingbot")
	.keyUp(txtUsername, Keys.SHIFT)
	.doubleClick(txtUsername)
	.contextClick()
	.build();
	
seriesOfActions.perform();
```

## Sidebar

### TestingBot Cloud Testing

Run automated, manual and visual tests on remote browsers and devices. Sign up for a free trial.

[Free Trial](https://testingbot.com/users/sign_up)

### Latest articles

[![Handling Captcha during Automated Testing](https://testingbot.com/assets/resources/articles/7-b54c5d487c7cc09e40ed52a5c59ecc06e26e172e66e215a8f7fb3e6c7d1c1de3.webp)](https://testingbot.com/resources/articles/automated-testing-with-captcha)

#### [Handling Captcha during Automated Testing](https://testingbot.com/resources/articles/automated-testing-with-captcha)

We will discuss possible solutions to run automated tests...

[Read article →](https://testingbot.com/resources/articles/automated-testing-with-captcha)

[![Cypress and Cucumber Browser Testing](https://testingbot.com/assets/resources/articles/6-495c5e14cb4bf1e282858fc7e2cfcbe89775e3790b88d34c15bf9a6f36d387d4.webp)](https://testingbot.com/resources/articles/cypress-cucumber-testing)

#### [Cypress and Cucumber Browser Testing](https://testingbot.com/resources/articles/cypress-cucumber-testing)

Cypress and Cucumber is a great combination to write clea...

[Read article →](https://testingbot.com/resources/articles/cypress-cucumber-testing)

[![TestNG automation with Selenium](https://testingbot.com/assets/resources/articles/5-46a5444bcdd6b07d6bbe8e9e58be5093fe3aaa8d1a7939b89291d7173d4e4c74.webp)](https://testingbot.com/resources/articles/testng-selenium)

#### [TestNG automation with Selenium](https://testingbot.com/resources/articles/testng-selenium)

TestNG is a popular Java Test Framework which allows you ...

[Read article →](https://testingbot.com/resources/articles/testng-selenium)

## Other Articles

[![Handling Captcha during Automated Testing](https://testingbot.com/assets/resources/articles/7-b54c5d487c7cc09e40ed52a5c59ecc06e26e172e66e215a8f7fb3e6c7d1c1de3.webp)](https://testingbot.com/resources/articles/automated-testing-with-captcha "Handling Captcha during Automated Testing")

### [Handling Captcha during Automated Testing](https://testingbot.com/resources/articles/automated-testing-with-captcha)

We will discuss possible solutions to run automated tests with Captcha enabled websites.

[Read article →](https://testingbot.com/resources/articles/automated-testing-with-captcha)

[![Cypress and Cucumber Browser Testing](https://testingbot.com/assets/resources/articles/6-495c5e14cb4bf1e282858fc7e2cfcbe89775e3790b88d34c15bf9a6f36d387d4.webp)](https://testingbot.com/resources/articles/cypress-cucumber-testing "Cypress and Cucumber Browser Testing")

### [Cypress and Cucumber Browser Testing](https://testingbot.com/resources/articles/cypress-cucumber-testing)

Cypress and Cucumber is a great combination to write clean and precise end-to-end browser tests.

[Read article →](https://testingbot.com/resources/articles/cypress-cucumber-testing)

[![TestNG automation with Selenium](https://testingbot.com/assets/resources/articles/5-46a5444bcdd6b07d6bbe8e9e58be5093fe3aaa8d1a7939b89291d7173d4e4c74.webp)](https://testingbot.com/resources/articles/testng-selenium "TestNG automation with Selenium")

### [TestNG automation with Selenium](https://testingbot.com/resources/articles/testng-selenium)

TestNG is a popular Java Test Framework which allows you to write clean Selenium tests with built-in parallelisation.

[Read article →](https://testingbot.com/resources/articles/testng-selenium)

[![blank](https://testingbot.com/assets/blank-765bda2d39d03b623c68515d78259503eb9ca884105d9fff58b2693418720ea1.gif)](https://testingbot.com/resources/articles/cross-browser-testing-competitors "TestingBot vs Browserstack and SauceLabs - Competitor analysis")

### [TestingBot vs Browserstack and SauceLabs - Competitor analysis](https://testingbot.com/resources/articles/cross-browser-testing-competitors)

Why would you choose TestingBot instead of an other company like SauceLabs or BrowserStack?

[Read article →](https://testingbot.com/resources/articles/cross-browser-testing-competitors)

## Ready to start testing?
[Start a free trial](https://testingbot.com/users/sign_up)
