I came across this solution to my initial problem, which was to simulate an ENTER or RETURN key press using Selenium WebDriver.
However, in my code, I strictly want to use only one of the two WebElement.sendKeys(Keys.ENTER);
vs WebElement.sendKeys(Keys.RETURN);
.
What is the best practice when doing the same, since there seems to be divided opinion about using enter or return, since both work MOST of the time? In what scenarios would one or the other not work, and is there one which would ALWAYS work?
As a performancewise I do not get any change on both of these,
But yes I know one difference on them
Keys.Enter
is used to enter key on the number pad
while
Keys.Return
is used to one next to the letters
Generally I have preferred Keys.Enter
as sometimes in some browser Keys.Return
is not worked for me
Let us analyze Keys.ENTER
and Keys.RETURN
in details.
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 there 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 OSX) 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.
Wiki References : Enter Key
Carriage Return
As yourself, I failed to find a good explanation to this question online so I tested it myself using this Keyboard Events tester.
driver.get("https://dvcs.w3.org/hg/d4e/raw-file/tip/key-event-test.html");
WebElement textArea = driver.findElement(By.id("input"));
textArea.sendKeys(Keys.ENTER);
textArea.sendKeys(Keys.RETURN);
As a result I've got this output (this is Keys.ENTER
followed by Keys.RETURN
):
So it seems like there is no difference between these two options.