How to use Actions Class in Selenium

In this article, you will learn about the Actions class and their example. Selenium provides different methods to perform click, input text, and select from dropdowns and tables. However, there are such complex actions that we need to perform like drag and drop, double click, right-click, and, many more. These actions can not be handled by simple web element commands. To handle those types of action, selenium provides Actions Class. Actions class is used to handle keyboard and mouse events.

Selenium Previous Article – Interact with Web Components in Selenium

Action Methods

There are some Actions methods that are given below – 

MethodsDescription
clickAndHold()Clicks an element without releasing it.
contextClick()Performs Mouse right-click on a given element
doubleClick()Performs double click on a given element
dragAndDrop(source,  target)Click and hold the source element and move to the target location and then release
sendKeys()Sends a series of Keys to the given element.
keyDown(KEY)Perform press the given KEY and do not release.KEY – Keys.ALT, Keys.SHIFT, Keys.CONTROL, etc.
keyUp(KEY)Perform release of the given KEY.
moveByOffset(X-offset, Y-offset)Moves the mouse to the given offset (X, Y)
moveToElement(WebElement)Moves the mouse to the middle of the given element.

To use the Actions Class in Java you need to import the following Actions class first –

import org.openqa.selenium.interactions.Actions

Selenium provides Keyboard Actions and Mouse Actions. Let’s look for each, one by one.

Keyboard Actions

It helps to interact with a web page using any key input device. There are only two types of actions that can be performed using the keyboard i.e. PRESS or RELEASE the key.

Keys

Every key is represented by a Unicode value. You can see the full list of Unicode values here.

Key Down

It presses a key after focusing on the target element.

Scenario – Input the name into the text field while using the SHIFT key.

driver.get("https://letcode.in/edit/");
WebElement input = driver.findElement(By.id("fullName"));
Actions actions = new Actions(driver);
actions.keyDown(input, Keys.SHIFT)
        .sendKeys("shubham")
        .build().perform();

Key Up

It releases a key after focusing on the target element.

Scenario – Append the text after releasing the SPACE key, and later Press the TAB key.

driver.get("https://letcode.in/edit/");
WebElement input = driver.findElement(By.id("join"));
Actions actions = new Actions(driver);
actions.keyDown(input, Keys.SPACE)
        .keyUp(Keys.SPACE)
        .sendKeys("tester.")
        .keyDown(Keys.TAB)
        .keyUp(Keys.TAB)
        .build().perform();

Send Keys

It sends the key sequence after focusing on the target element.

Scenario – Input the text into the text field using the Actions class.

driver.get("https://letcode.in/edit/");
WebElement input = driver.findElement(By.id("fullName"));
Actions actions = new Actions(driver);
actions.sendKeys(input, "Shubham")
        .build().perform();

Mouse Actions

It helps to interact with a web page using a mouse device. There are different types of actions that can be performed using the mouse i.e. PRESS DOWN or RELEASE mouse buttons, and moving the mouse.

Click and Hold

It presses the left button of the mouse on a target element without releasing it.

Scenario – Press a button and hold.

driver.get("https://letcode.in/buttons/");
WebElement button = driver.findElements(By.id("isDisabled")).get(1);
Actions actions = new Actions(driver);
actions.clickAndHold(button)
        .build().perform();

Context Click

It presses the right button of the mouse on a target element.

Scenario – Perform right-click on a button

driver.get("https://demoqa.com/buttons");
WebElement button = driver.findElement(By.id("rightClickBtn"));
Actions actions = new Actions(driver);
actions.contextClick(button)
        .build().perform();

Double Click

It presses the left button of the mouse twice on a target element.

Scenario – Perform a double click on a button.

driver.get("https://demoqa.com/buttons");
WebElement button = driver.findElement(By.id("doubleClickBtn"));
Actions actions = new Actions(driver);
actions.doubleClick(button)
        .build().perform();

Drag and Drop

It moves one element from the source to the middle of the target location.

Scenario – Drag an element to the target location.

driver.get("https://demoqa.com/droppable");
WebElement source = driver.findElement(By.id("draggable"));
WebElement target = driver.findElement(By.id("droppable"));
Actions actions = new Actions(driver);
actions.dragAndDrop(source, target)
        .build().perform();

Move to Element

It moves the mouse pointer to the middle of the given element.

Scenario – Perform hover on an element.

driver.get("https://demoqa.com/tool-tips");
WebElement button = driver.findElement(By.id("toolTipButton"));
Actions actions = new Actions(driver);
actions.moveToElement(button)
        .build().perform();

Move by Offset

It moves the mouse pointer to the middle of the given offset X and Y from the current pointer location.

Scenario – Perform move by offset.

Actions actions = new Actions(driver);
actions.moveByOffset(13, 100)
        .build().perform();

Conclusion

In this article, we have learned about the Selenium Actions class. We have looked at how to interact with a webpage using the Actions class. I hope this article simplifies the process to simulate the common user actions on websites and applications.

Resources

You might Like

💖 If you like this article please make sure to Like, Comment, and Share it with your friends and colleagues.

Follow us on our social networks –