Skip to main content

You are here

Keyboard operations in Selenium WebDriver

The Keys class library allows us to reference the non alphabet/ text keys on the keyboard. This applies to keyboard keys like the ARROW keys, ALT, CTRL etc. Of course for text entries like "John", we can simply write:


The following codes will call the browser, load the google website and then simulate making an entry of "1999" into the search text box.

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class KeyboardOperations {
         public static void main(String[] args) {
                WebDriver driver = new FirefoxDriver();
                driver.navigate().to(""); // Actual loading of website
               System.out.println("Make entries into Google Search using keyboard actions");
               WebElement searchInput = driver.findElement(By.className("gbqfif"));
               String allKeys = ""+ Keys.NUMPAD1 + Keys.NUMPAD9 + Keys.NUMPAD9 + Keys.NUMPAD9; // 1 + 9 + 9 + 9
               allKeys = allKeys + Keys.ENTER; // now add the ENTER key. We could also have done it above
               searchInput.sendKeys(allKeys); // send the key presses into the text box



No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.


Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes