Typing the Enter/Return key in Selenium
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
Related videos on Youtube
croixhaug
Updated on February 25, 2022Comments
-
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 over 14 yearsThis might help asynchrony.blogspot.com/2008/11/…
-
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 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 over 11 years@AlexOkrushko: yes, you are right
-
cigien over 2 yearsThis post is being discussed on meta.
-
-
James Broadhead over 12 yearsI believe that it's moved.
from selenium.webdriver.common.keys import Keys
(stackoverflow.com/questions/5503489/…) -
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 almost 12 yearsWebElement.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 over 11 yearsI 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 over 10 yearsThis is also true for a GWT input text field.
-
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 about 9 yearsIf the element has ID, it is better to use ID instead of xpath
-
abarisone almost 9 yearsCould you please elaborate more your answer adding a little more description about the solution you provide?
-
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 over 8 yearsThis answer work but accidently voted it down because I used the wrong number.
-
Chandrashekhar Swami about 8 yearsjust out of curiosity , why do you prefer writing 3 lines of code which you can write in a single line
-
RNS about 7 years@Ralph Thanks for your solution. It's worthy for upvote.
-
Guilherme over 6 yearsfor python:
from selenium.webdriver.common.keys import Keys
password_field.send_keys(Keys.ENTER)
-
YakovK over 5 yearsFor 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 over 3 yearsRe "keys function": Do you mean
send_keys()
(this is about Python)? -
Peter Mortensen over 3 yearsCan you link to the documentation?
-
Peter Mortensen over 3 yearsWhat list? Can you link to it?
-
Peter Mortensen over 3 yearsCan you link to documentation for the
Robot
class? -
Peter Mortensen over 3 yearsOr more generally, values from
Keys
(import can beselenium.webdriver.common.keys import Keys
),Keys.RETURN
in this case. -
Peter Mortensen over 3 yearsIsn't an "import" of sorts needed for
Keys
? In Python, it would be "from selenium.webdriver.common.keys import Keys
". -
Peter Mortensen over 3 yearsIsn't an "import" of sorts needed for
Keys
? In Python, it would be "from selenium.webdriver.common.keys import Keys
". -
Peter Mortensen over 3 yearsWhat language? Isn't an "import" of sorts needed for
Keys
? In Python, it would be "from selenium.webdriver.common.keys import Keys
". -
Peter Mortensen over 3 yearsWhat 'language'/context? Bash (command line)? Linux? Can you add some context to your answer?
-
Peter Mortensen over 3 yearsIs "webElement" literal?
-
Peter Mortensen over 3 yearsWhat language? Python?
-
Peter Mortensen over 3 yearsWhat language? Java?
-
Maurice Svay about 3 yearsIt's for HTML tests suites
-
zaheer almost 3 yearsthis answer works perfectly
driver.find_element_by_xpath('//xpath').send_keys(Keys.ENTER)
in case for search works perfectly.