Typing the Enter/Return key in Selenium

810,231

Solution 1

import org.openqa.selenium.Keys

WebElement.sendKeys(Keys.RETURN);

The import statement is for Java. For other languages, it is maybe different. For example, in Python it is from selenium.webdriver.common.keys import Keys

Solution 2

Java

driver.findElement(By.id("Value")).sendKeys(Keys.RETURN);

OR,

driver.findElement(By.id("Value")).sendKeys(Keys.ENTER);

Python

from selenium.webdriver.common.keys import Keys
driver.find_element_by_name("Value").send_keys(Keys.RETURN)

OR,

driver.find_element_by_name("Value").send_keys(Keys.ENTER)

OR,

element = driver.find_element_by_id("Value")
element.send_keys("keysToSend")
element.submit()

Ruby

element = @driver.find_element(:name, "value")
element.send_keys "keysToSend"
element.submit

OR,

element = @driver.find_element(:name, "value")
element.send_keys "keysToSend"
element.send_keys:return

OR,

@driver.action.send_keys(:enter).perform
@driver.action.send_keys(:return).perform

C#

driver.FindElement(By.Id("Value")).SendKeys(Keys.Return);

OR,

driver.FindElement(By.Id("Value")).SendKeys(Keys.Enter);

Solution 3

You can use either of Keys.ENTER or Keys.RETURN. Here are the details:

Usage:

  • Java:

    • Using Keys.ENTER:

      import org.openqa.selenium.Keys;
      driver.findElement(By.id("element_id")).sendKeys(Keys.ENTER);
      
    • Using Keys.RETURN:

      import org.openqa.selenium.Keys;
      driver.findElement(By.id("element_id")).sendKeys(Keys.RETURN);
      
  • Python:

    • Using Keys.ENTER:

      from selenium.webdriver.common.keys import Keys
      driver.find_element_by_id("element_id").send_keys(Keys.ENTER)
      
    • Using Keys.RETURN:

      from selenium.webdriver.common.keys import Keys
      driver.find_element_by_id("element_id").send_keys(Keys.RETURN)
      

Keys.ENTER and Keys.RETURN both are from org.openqa.selenium.Keys, which extends java.lang.Enum<Keys> and implements java.lang.CharSequence.


Enum Keys

Enum Keys is the representations of pressable keys that aren't text. These are stored in the Unicode PUA (Private Use Area) code points, 0xE000-0xF8FF.

Key Codes:

The special keys codes for them are as follows:

  • RETURN = u'\ue006'
  • ENTER = u'\ue007'

The implementation of all the Enum Keys are handled the same way.

Hence these is No Functional or Operational difference while working with either sendKeys(Keys.ENTER); or WebElement.sendKeys(Keys.RETURN); through Selenium.


Enter Key and Return Key

On computer keyboards, the Enter (or the Return on Mac OS X) in most cases causes a command line, window form, or dialog box to operate its default function. This is typically to finish an "entry" and begin the desired process and is usually an alternative to pressing an OK button.

The Return is often also referred as the Enter and they usually perform identical functions; however in some particular applications (mainly page layout) Return operates specifically like the Carriage Return key from which it originates. In contrast, the Enter is commonly labelled with its name in plain text on generic PC keyboards.


References

Solution 4

Now that Selenium 2 has been released, it's a bit easier to send an Enter key, since you can do it with the send_keys method of the selenium.webdriver.remote.webelement.WebElement class (this example code is in Python, but the same method exists in Java):

>>> from selenium import webdriver
>>> wd = webdriver.Firefox()
>>> wd.get("http://localhost/example/page")
>>> textbox = wd.find_element_by_css_selector("input")
>>> textbox.send_keys("Hello World\n")

Solution 5

In Python

Step 1. from selenium.webdriver.common.keys import Keys

Step 2. driver.find_element_by_name("").send_keys(Keys.ENTER)

Note: you have to write Keys.ENTER

Share:
810,231

Related videos on Youtube

croixhaug
Author by

croixhaug

Updated on February 25, 2022

Comments

  • croixhaug
    croixhaug about 2 years

    I'm looking for a quick way to type the Enter or Return key in Selenium.

    Unfortunately, the form I'm trying to test (not my own code, so I can't modify) doesn't have a Submit button. When working with it manually, I just type Enter or Return. How can I do that with the Selenium type command as there is no button to click?

    • Jonathan Parker
      Jonathan Parker over 14 years
    • Ripon Al Wasim
      Ripon Al Wasim over 11 years
      @croixhaug: What are you using? Selenium RC or WebDriver (Selenium 2)? What about language? Java? C#? or what?
    • Alex Okrushko
      Alex Okrushko over 11 years
      @RiponAlWasim, in 2009 (when the question was asked) there were no WebDriver. Also the answers for both have been here for a while...
    • Ripon Al Wasim
      Ripon Al Wasim over 11 years
      @AlexOkrushko: yes, you are right
    • cigien
      cigien over 2 years
      This post is being discussed on meta.
  • James Broadhead
    James Broadhead over 12 years
    I believe that it's moved. from selenium.webdriver.common.keys import Keys (stackoverflow.com/questions/5503489/…)
  • Ralph
    Ralph over 12 years
    @HJames Broadhead: I have checked it, the Keys class for the actual JAVA (2.17.0) is still org.openqa.selenium.Keys
  • Ripon Al Wasim
    Ripon Al Wasim almost 12 years
    WebElement.sendKeys(Keys.RETURN); this code is perfect for WebDriver (not for Selenium RC). More details example for google search: driver.findElement(By.id("gbqfq")).clear(); driver.findElement(By.id("gbqfq")).sendKeys("Ripon Al Wasim"); driver.findElement(By.id("gbqfq")).sendKeys(Keys.RETURN);
  • NoBrainer
    NoBrainer over 11 years
    I know that return is different than enter, but how is Keys.ENTER different? (I would think that Keys.RETURN would simply make it more obvious that it is a bot doing the action?)
  • joescii
    joescii over 10 years
    This is also true for a GWT input text field.
  • omikron
    omikron over 10 years
    @NoBrainer: Quick look at imported file will answer your question: RETURN = '\ue006' ENTER = '\ue007'. But why? Some relic or OS differences.
  • Ripon Al Wasim
    Ripon Al Wasim about 9 years
    If the element has ID, it is better to use ID instead of xpath
  • abarisone
    abarisone almost 9 years
    Could you please elaborate more your answer adding a little more description about the solution you provide?
  • Ralph
    Ralph over 8 years
    @NoBrainer: Enter and Return are different keys, have a look at the image of this wikipedia article: en.wikipedia.org/wiki/Enter_key
  • Martin Kersten
    Martin Kersten over 8 years
    This answer work but accidently voted it down because I used the wrong number.
  • Chandrashekhar Swami
    Chandrashekhar Swami about 8 years
    just out of curiosity , why do you prefer writing 3 lines of code which you can write in a single line
  • RNS
    RNS about 7 years
    @Ralph Thanks for your solution. It's worthy for upvote.
  • Guilherme
    Guilherme over 6 years
    for python: from selenium.webdriver.common.keys import Keys password_field.send_keys(Keys.ENTER)
  • YakovK
    YakovK over 5 years
    For Python, I found that appending "\n" to the input string is the most straightforward way to go. It worked in a search field.
  • Peter Mortensen
    Peter Mortensen over 3 years
    Re "keys function": Do you mean send_keys() (this is about Python)?
  • Peter Mortensen
    Peter Mortensen over 3 years
    Can you link to the documentation?
  • Peter Mortensen
    Peter Mortensen over 3 years
    What list? Can you link to it?
  • Peter Mortensen
    Peter Mortensen over 3 years
    Can you link to documentation for the Robot class?
  • Peter Mortensen
    Peter Mortensen over 3 years
    Or more generally, values from Keys (import can be selenium.webdriver.common.keys import Keys), Keys.RETURN in this case.
  • Peter Mortensen
    Peter Mortensen over 3 years
    Isn't an "import" of sorts needed for Keys? In Python, it would be "from selenium.webdriver.common.keys import Keys".
  • Peter Mortensen
    Peter Mortensen over 3 years
    Isn't an "import" of sorts needed for Keys? In Python, it would be "from selenium.webdriver.common.keys import Keys".
  • Peter Mortensen
    Peter Mortensen over 3 years
    What language? Isn't an "import" of sorts needed for Keys? In Python, it would be "from selenium.webdriver.common.keys import Keys".
  • Peter Mortensen
    Peter Mortensen over 3 years
    What 'language'/context? Bash (command line)? Linux? Can you add some context to your answer?
  • Peter Mortensen
    Peter Mortensen over 3 years
    Is "webElement" literal?
  • Peter Mortensen
    Peter Mortensen over 3 years
    What language? Python?
  • Peter Mortensen
    Peter Mortensen over 3 years
    What language? Java?
  • Maurice Svay
    Maurice Svay about 3 years
    It's for HTML tests suites
  • zaheer
    zaheer almost 3 years
    this answer works perfectly driver.find_element_by_xpath('//xpath').send_keys(Keys.ENTER‌​) in case for search works perfectly.