9ab. WebDriver – Using Actions Class

Lights, camera, action! Yes, today its all about Actions. Oh, I am not talking about those fight sequences you watch in movies but about the keyboard and mouse actions. 😛 Selenium WebDriver provides a user-facing API for performing complex user gestures. We want automation everywhere! So, instead of using keyboard and mouse directly, we can use Actions Class for performing both basic viz., click, sendKeys and complex actions such as dragAndDrop etc.

Some of the available keyboard and mouse methods are,

1. click(WebElement target)

Parameter: target – element to click.

Description: Clicks in the middle of the given target.

2. clickAndHold()

Description: Clicks at the current mouse location and does not release.

3. doubleClick(WebElement target)

Parameter: target – element to click.

Description: Performs a double click at the middle of the given target element. This is equivalent to:  Actions.moveToElement(element).doubleClick();

4. dragAndDrop(WebElement source, WebElement target)


Source – element to be dragged i.e. where button down is to be emulated at

Target – element to move to and release the mouse at

Description: performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.

5. dragAndDropBy(WebElement source, int xOffset, int yOffset)


source – element to emulate button down at.

xOffset – horizontal move offset.

yOffset – vertical move offset.

Description: performs click-and-hold at the location of the source element, moves on the x-axis by a given xOffset and on the y-axis by the given yOffset, then releases the mouse.

6. keyDown(java.lang.CharSequence modifier_key)


Modifier_key – It can be either Keys.SHIFT, Keys.ALT or Keys.CONTROL. If the provided key is none of those, IllegalArgumentException is thrown.

Description: Performs a modifier key press and does not release the key. Subsequent interactions may assume that the key is kept pressed. Note that the modifier key is never released implicitly – either keyUp(theKey) or sendKeys(Keys.NULL) must be called to release the modifier key.

7. moveByOffset(int xOffset, int yOffset)


xOffset – horizontal offset. A negative value means moving the mouse left.

xOffset – vertical offset. A negative value means moving the mouse up.

Description: Moves the mouse from its current position (or 0,0) by the given xOffset on the x-axis and yOffset on the y-axis.

8. moveToElement(WebElement target)


target – element to move to.

Description: Moves the mouse to the middle of the target element.

9. perform()

Description: performs or executes all the actions.

10. release()

Description: Releases the depressed left mouse button at the current mouse location.

Overall picture

Let us see a test case implementing some of the methods that we just discussed,


  1. Open Chrome browser
  2. Navigate to the demo site
  3. Using Actions Class perform the following keyboard and mouse actions,
    1. Locate and click on the ‘Bicycle’ checkbox
    2. Locate the text, ‘Message’ and double-click to highlight it
    3. Type ‘hi there’ in the text box in uppercase
    4. Drag click() from the first position and drop it in the third position in the sortable list
  4. Verify the output in the Chrome browser
  5. Verify Eclipse IDE console output screen for any printed messages and JUnit pane for success result

JUnit code for this scenario is,

import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class TestActions {
	// Declaring variables
	private WebDriver driver;
	private String baseUrl;

	public void setUp() throws Exception {
		// System property set up for Chrome driver
		System.setProperty("webdriver.chrome.driver", "browser-drivers\\chromedriver.exe");
		// Create a new instance for the class ChromeDriver
		// that implements WebDriver interface
		driver = new ChromeDriver();
		// Implicit wait for 5 seconds
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		// Assign the URL to be invoked to a String variable
		baseUrl = "https://chandanachaitanya.github.io/selenium-practice-site/";
	public void testKeyboardAndMouseActions() throws Exception {
		// Open baseUrl in Chrome browser window
		// Locate 'Bicycle' checkbox using name
		WebElement bicyle = driver.findElement(By.name("vehicle1"));
		// Locate 'Message' textbox using id
		WebElement messageTextBox = driver.findElement(By.id("enterText"));
		// Locate 'click()' using id
		WebElement element = driver.findElement(By.id("click"));
		//Create an instance of the Actions Class
		Actions actions = new Actions(driver);
                // Perform multiple actions
		// Type 'hi there' in uppercase in the text box
			.sendKeys(messageTextBox, "hi there")
		// Click 'Bicycle' checkbox
		// Print a message to console
		System.out.println("Bicylce checkbox clicked.");
		// Double click/highlight the text, 'Message'
		// Print the text of the element that will be dragged and dropped
		System.out.println("Element that is dragged : " + element.getText());
		// Drag and drop 'click()' using Actions class
			.dragAndDropBy(element, 50, 100)

	} // End of @Test

	public void tearDown() throws Exception {
		// Close the Chrome browser
		System.out.println("Closing the driver");


Let us decipher some complex actions.

1. Highlight the text, ‘Message’:


Move the focus to the text, ‘Message’ and double-click on it to highlight. Use perform() method to execute the actions.

2. Type ‘hi there’ in uppercase in the text box

	.sendKeys(text, "hi there")

First move the focus to the text box, hold the SHIFT key down, type the text “hi there” so that it gets typed in uppercase and then release the SHIFT key with the help of keyup method.

3. Drag and drop ‘click()’

This can be done in two ways,

	.dragAndDropBy(element, 50, 100)

The simplest way is to use the dragAndDropBy method where we can specify the element to be dragged along with the xOffset and yOffset.

If you want to take the complex route and look like a ninja,

	.moveByOffset(50, 100)

Click on the target element, html() in our case and make sure not to release the click. Move by a given xOffset and yOffset, then release the mouse click so that the element is dropped in the new location. As a result, the element is dragged and dropped.

Execution Result:

Comments are provided for each line of code making the rest of it self-explanatory. Sprinkling some visuals of the demo site output,

Actions output 1

Bicycle checkbox is located and checked as expected.

Actions output 2

The text Message is double-clicked, as a result, highlighted. In the text box, ‘hi there’ is keyed in uppercase. From the sortable list, click() is dragged from its default first position and dropped at the 3rd position.

In JUnit pane, green bar shows that the test case is executed successfully. Also, the console window shows the absence of any errors along with all the printed messages as expected.

Actions eclipse IDE output

Time to bring out the ninja within you. Experiment with as many Action Class methods as possible and have some fun!

All the code discussed above is available in the GitHub repo, ‘WebDriver’ folder. You can star and fork the repository for convenience. Please go through the README.md file for clear instructions.

Have a great day!

Lakshmi Chandana is a Software professional + passion-fueled blogger + novel-reader + artist + tutor to make your day a little brighter than it was before! She is thrilled you are here! She is on a mission to make sure learning sticks but with the fun part kept intact. She uses certain tricks called BrainBells (inspired from barbells and dumbbells used for workout) to achieve this and she says, “this is not the hardest job! As once minions said, it for sure is working in a bubble wrap factory. Imagine the self-control needed!” So, dive in to explore the fun-filled World of Learning!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search