Press keyboard keys in selenium

During automation, we are often required to press enter, control, tab, arrow keys, function keys and other non-text keys as well from keyboard. In this post, we will find how to simulate pressing of these non-text keys using selenium webdriver in java. Here, we will be using Keys enum provided by Selenium webdriver for all the non-text keys.

Press Enter/Return Key in Selenium

For pressing Enter key over a textbox we can pass Keys.ENTER or Keys.RETURN to the sendKeys method for that textbox.

WebElement textbox = driver.findElement("idOfElement"));


WebElement textbox = driver.findElement("idOfElement"));

Similarly, we can use Keys enum for different non-text keys and pass them to the sendKeys method. The following table has an entry for each of the non-text key present in a keyboard.

Keyboard's KeyKeys enum's value
Arrow Key - DownKeys.ARROW_DOWN
Arrow Key - UpKeys.ARROW_LEFT
Arrow Key - LeftKeys.ARROW_RIGHT
Arrow Key - RightKeys.ARROW_UP
Ctrl KeyKeys.CONTROL
Alt keyKeys.ALT
Enter KeyKeys.ENTER
Shift KeyKeys.SHIFT
Tab KeyKeys.TAB
Equals KeyKeys.EQUALS
Esc KeyKeys.ESCAPE
Home KeyKeys.HOME
Insert KeyKeys.INSERT
PgUp KeyKeys.PAGE_UP
Function Key F1Keys.F1
Function Key F2Keys.F2
Function Key F3Keys.F3
Function Key F4Keys.F4
Function Key F5Keys.F5
Function Key F6Keys.F6
Function Key F7Keys.F7
Function Key F8Keys.F8
Function Key F9Keys.F9
Function Key F10Keys.F10
Function Key F11Keys.F11
Function Key F12Keys.F12