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);
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|
|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.
Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in test automation, performance testing, big data, and CI-CD. He brings his decade of experience to his current role where he is dedicated to educating the QA professionals. You can connect with him on LinkedIn.
17 thoughts on “Press Keys in Selenium – ENTER, TAB, SPACE, CONTROL, ARROW, FUNCTION Keys”
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
//Release key Ctrl+Shift+i
You might also think about using CHORD
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=220.127.116.11, 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)
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.
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:
How we can navigate by just press “M” letter as shortcut for Button through selenium
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