Press Keys in Selenium – ENTER, TAB, SPACE, CONTROL, ARROW, FUNCTION Keys
During automation, we are often required to press enter, control, tab, arrow keys, function keys, and other non-text keys as well from the keyboard. In this post, we will find how to simulate the pressing of these non-text keys using Selenium WebDriver in Java. Here, we will be using the 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(By.id("idOfElement"));
textbox.sendKeys(Keys.ENTER);
Or
WebElement textbox = driver.findElement(By.id("idOfElement"));
textbox.sendKeys(Keys.RETURN);
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 Key | Keys enum’s value |
---|---|
Arrow Key – Down | Keys.ARROW_DOWN |
Arrow Key – Up | Keys.ARROW_UP |
Arrow Key – Left | Keys.ARROW_LEFT |
Arrow Key – Right | Keys.ARROW_RIGHT |
Backspace | Keys.BACK_SPACE |
Ctrl Key | Keys.CONTROL |
Alt key | Keys.ALT |
DELETE | Keys.DELETE |
Enter Key | Keys.ENTER |
Shift Key | Keys.SHIFT |
Spacebar | Keys.SPACE |
Tab Key | Keys.TAB |
Equals Key | Keys.EQUALS |
Esc Key | Keys.ESCAPE |
Home Key | Keys.HOME |
Insert Key | Keys.INSERT |
PgUp Key | Keys.PAGE_UP |
PgDn Key | Keys.PAGE_DOWN |
Function Key F1 | Keys.F1 |
Function Key F2 | Keys.F2 |
Function Key F3 | Keys.F3 |
Function Key F4 | Keys.F4 |
Function Key F5 | Keys.F5 |
Function Key F6 | Keys.F6 |
Function Key F7 | Keys.F7 |
Function Key F8 | Keys.F8 |
Function Key F9 | Keys.F9 |
Function Key F10 | Keys.F10 |
Function Key F11 | Keys.F11 |
Function Key F12 | Keys.F12 |
That’s all we have for now, comment below if you have any questions. Also, don’t forget to check our Step-by-Step selenium tutorial.