Home » Enterprise Java » Selenium » Selenium Keyboard Events Example

About Sarad Dhungel

Avatar photo
I am a graduate in Computer Engineering from Howard University. Third place award winner in Intel-Cornell Cup. Passionate about new emerging software and technology. During free time, I enjoy reading about politics, business, spirituality, technology and traveling.

Selenium Keyboard Events Example

1. Introduction

In this tutorial we will be discussing about the Advance User Interactions API. Using these APIs, we can perform actions on a web page similar to a user would interact on the user using keyboard and mouse.

The Actions interface and Action classes are two modules of the API that needs to be implemented to make use of keyboard and mouse events like Drag and Drop or selecting multiple elements with Control Key.

2. Action interface and Actions Class

Actions class implement the Action interface. Action interface only one method: perform(). The action method gets the arguments as constructor and then implementing class decides what interaction should be done on the webpage. For example finding an Element, passing keys using sendkeys and highlighting it.

We can implement action actions methods by importing org.openqa.selenium.interactions.Actions and org.openqa.selenium.interactions.Action

Then configure it by creating an object of Actions class like below:
Actions build = new Actions(driver);

3. Available actions for Keyboard and Mouse Events

  • ButtonReleaseAction – Releases pressed mouse button.
  • ClickAction – clicks on an element similar to WebElement.click().
  • ClickAndHoldAction – holds the left mouse button.
  • ContextClickAction – similar to clicking on right button contextual menu
  • DoubleClickAction– similar to double clicking.
  • KeyDownAction – similar to holding modifier keys like tab, shift and control
  • KeyUpAction – releases modifier keys.
  • MoveMouseAction – similar to moving the mouse from current location to another.
  • MoveToOffsetAction – moves the mouse from one location to another using x and y offset.
  • SendKeysAction – similar to WebElement.sendKey() to send keys in form of sequence of characters.

In order to achieve different operations that are performed by actions classes, we can implement the methods available for keyboard and mouse interfaces.

3.1 Methods of Keyboard Interface:

  1. sendKeys(onElement, charSequence) – send keys to the browser similarly a user would do using keyboard. Keys are send in forms of sequence of characters.
  2. pressKeys() – used to send special keys of the keyboard like “f1”, “shift”, “tab” etc.
  3. releaseKey() – releases key on the keyboard and presskeys.


package com.javacodegeeks.SeleniumKeyboardEvents;

import java.util.concurrent.TimeUnit;

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.Action;
import org.openqa.selenium.interactions.Actions;

public class KeyboardEvents {
public static void main(String[] args) {
	     String exePath = "/Users/saraddhungel/Downloads/chromedriver";
	     System.setProperty("webdriver.chrome.driver", exePath);
         WebDriver driver = new ChromeDriver();

		 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		 WebElement text = driver.findElement(By.name("q"));
		 Actions make  = new Actions(driver);
		 Action kbEvents = make.keyDown(text, Keys.SHIFT).sendKeys("Java Code Geeks")
	    .keyUp(text, Keys.SHIFT).doubleClick().contextClick().build();

The “Java Code Geeks” keyword is sent to the search box of the google site. Thus, it is changed to UPPERCASE with KeyDown() method and doubleClick() method double clicks on it to highlight the text and contextClick() does event of double click on the text and thus contextual menu is displayed.

4. Conclusion

This example set was a demonstration of the Advance User Interactions API available in Selenium WebDriver and how can we use them to make use of Keyboard and Mouse events on the web similar to user’s interaction to web.

5. Download the Eclipse Project

This was an example of Selenium Keyboard Event

You can download the source code of this example here: Selenium Keyboard Event

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

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

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

Nice tutorial