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.
Hey there is there a way of saying CONTROL+SHIFT+’I’
This command opens gmail and I would like to use it for my code, do you know how I can do this? Thank you!
You can use Robot class for this-
Robot robot = new Robot();
//Press key Ctrl+Shift+i
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_I);
//Release key Ctrl+Shift+i
robot.keyRelease(KeyEvent.VK_I);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_CONTROL);
You might also think about using CHORD
example:
myField.sendKeys(webdriver.Key.chord(webdriver.Key.CONTROL, webdriver.Key.SHIFT, ‘l’))
I got this error when using the above lines , can you help me ?
System.IO.FileLoadException: Could not load file or assembly ‘IKVM.Runtime, Version=8.2.0.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Hi,
For Arrow Key – Up we are using enum “Keys.ARROW_LEFT”
Is it right ?
Thanks! corrected, it was a typo.
Is there possible to use END option/key to delete the word?
Why don’t you use, Keys.DELETE for the same.
Keys.END will place the cursor at the end of the text.
shift + end + backspace
Keys.ENTER ,Is Keys a class in selenium ? What is ENTER ? Is Enter a static variable of class Keys ?
Keys is an enum and ENTER is the enum constant.
how can we press Fn+F7 key?
how can i preform “right click by mouse and down button by keyboard” using selenium??
This doc may help:
https://www.selenium.dev/selenium/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html#module-selenium.webdriver.common.action_chains
How we can navigate by just press “M” letter as shortcut for Button through selenium
driver.findElement(By.id(“query”)).sendKeys(“testng”, Keys.ENTER);
How can i enter three spaces in a field in feature file
By using keys.space in
Ex: when the user enters presskeywords.keys.space????
is there any option to enter comma
is there a way to send operation SHIFT+TAB using python
How to delete the values in a grid using Selenium WebDriver and share the sample scripts
hi
how we can preform ctrl+enter keys?