How to Handle Keyboard Actions in Selenium?

black lighted gaming keyboard
Reading Time: 3 minutes

In this blog we will learn how we can handle Keyboard Actions using selenium Webdriver.Before starting the blog first we must know what is Actions Class how it is used? So,Let’s start.

What is the actions class in Selenium Webdriver?

Actions like clicking a button, similarly entering a keyword in the search bar are prime examples of how we use a mouse or keyboard. Hence ,these interactions are done through the mouse and the keyboard can be automated by using the Actions class in Selenium.

Consequently ,the Actions class is the user-facing API for emulating complex action events. We can directly use this class rather than using the input devices, i.e. keyboard and mouse.

Keyboard Actions

The various keyboard actions that are provided by the Actions class are:-

send_keys(*keys_to_send)

Send keys to the element that is currently in focus.

key_down(valueelement=None)

Sends a key press without performing the release. It should only be used with modifier keys like Control, Alt, and Shift.

key_up(valueelement=None)

Releases a key. It should only be used with modifier keys like Control, Alt, and Shift.

How to simulate keyboard typing

sendKeys()

Using the Actions class in Selenium, we can implement the sendKeys() method to type specific values in the application.

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class KeyboardActions {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
WebDriver driver = new ChromeDriver();

//sendKeys()
driver.get("https://google.com");
driver.findElement(By.name("q")).sendKeys("selenium"+Keys.ENTER);

Thread.sleep(2000);
driver.quit();
}
}

How to simulate pressing key combination

keyDown()

This method simulates a keyboard action when a specific keyboard key needs to press. So, whenever you need to press a key and then perform specific other actions, we can use the keyDown() method to keep the key pressed.

Code:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class KeyboardActions {
    public static void main(String[] args) throws InterruptedException{
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
        WebDriver driver = new ChromeDriver();

        //keyDown()
        driver.get("https://google.com");
        driver.findElement(By.name("q")).sendKeys("selenium"+Keys.ENTER);
        Actions actionProvider=new Actions(driver);
        Action keyDown = actionProvider.keyDown(Keys.CONTROL).sendKeys("a").build();
        keyDown.perform();
        Thread.sleep(2000);
        driver.quit();
    }
}

How to release pressed keys

keyUp()

This method first focusses on the web element, which gets passed as a parameter to the method. Then, it releases the mentioned key on that Web Element.

Code:

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 KeyboardActions {
public static void main(String[] args) throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://google.com");
Actions actions = new Actions(driver);
WebElement searchBox = driver.findElement((By.name("q")));
actions.keyDown(Keys.SHIFT).sendKeys(searchBox,"selenium")
.keyUp(Keys.SHIFT).sendKeys("selenium").perform();
Thread.sleep(2000);
searchBox.clear();

Thread.sleep(2000);
driver.quit();
}
}

How to clear the text

clear()

Thanks for reading!!

References:

https://www.selenium.dev/documentation/webdriver/actions_api/keyboard/

Written by 

Sakshi Sneha is Software Consultant in Knoldus Inc. She is always eager to learn new and advanced concepts in order to upskill herself.