Press ENTER,space, Function in Selenium

Press Keys in Selenium – ENTER, TAB, SPACE, CONTROL, ARROW, FUNCTION Keys

Last updated on

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 KeyKeys enum’s value
Arrow Key – DownKeys.ARROW_DOWN
Arrow Key – UpKeys.ARROW_UP
Arrow Key – LeftKeys.ARROW_LEFT
Arrow Key – RightKeys.ARROW_RIGHT
BackspaceKeys.BACK_SPACE
Ctrl KeyKeys.CONTROL
Alt keyKeys.ALT
DELETEKeys.DELETE
Enter KeyKeys.ENTER
Shift KeyKeys.SHIFT
SpacebarKeys.SPACE
Tab KeyKeys.TAB
Equals KeyKeys.EQUALS
Esc KeyKeys.ESCAPE
Home KeyKeys.HOME
Insert KeyKeys.INSERT
PgUp KeyKeys.PAGE_UP
PgDn KeyKeys.PAGE_DOWN
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

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.

21 thoughts on “Press Keys in Selenium – ENTER, TAB, SPACE, CONTROL, ARROW, FUNCTION Keys”

  1. 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!

    Reply
    • 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);

      Reply
      • You might also think about using CHORD

        example:
        myField.sendKeys(webdriver.Key.chord(webdriver.Key.CONTROL, webdriver.Key.SHIFT, ‘l’))

        Reply
      • 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)

        Reply
  2. 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????

    Reply

Leave a Comment