3

I am using Selenium Server (v2.21) and Selenium Java Client (v.2.21.0) to automate a web form that needs to have the Enter key pressed after each entry, since fields are exposed based on the value entered. So based on the solution here, I've been trying different ways to enter a string into the form and press Enter - here are the ones that I've tried:

// type field value
selenium.type("program", "MIC HOMEOWNERS");

// ** not working: selenium.keyPress("program", "\\13");
// ** not working: selenium.select("program", "Program");
// ** not working: selenium.keyPressNative(Keys.ENTER.toString());
// ** not working: selenium.keyDown("program", "13");

It would seem like this is the most logical solution (selenium.keyPressNative(Keys.ENTER)), but the compiler throws an error if you don't add the .toString, since keyPressNative is expecting a String.

The actual form code:

<label  >Program</label> 
    <input id="program" name="program1" class="readonly-bg" readonly="readonly" type="text" value="MIC HOMEOWNERS" size="12"/>
    <input id="program" name="program" type="hidden" value="601"/>
        <script type="text/javascript">  
            Spring.addDecoration(new Spring.ElementDecoration({  
                elementId : "program",  
                widgetType : "dijit.form.ValidationTextBox",  
                widgetAttrs : { 
                    trim:true ,
                    required : true
                }}));  
        </script>
<br>

How do I emulate the press of the Enter key?

1
  • What are you using? Selenium WebDriver or RC? Commented Sep 2, 2014 at 10:27

8 Answers 8

7

I am using the following code to click the Escape or Enter.

try {
    Thread.sleep(700);
} catch (InterruptedException e) {
    selenium.keyPressNative("27"); // Escape
    selenium.keyPressNative("10"); // Enter 
}   

we need to pause the selenium till previous command get executed successfully. So I am using the sleep() method. For my test case, I required to pause for 700 MillSec. Bases on your requirement you need to change the value.

Sign up to request clarification or add additional context in comments.

1 Comment

And a reason: The Keys enum is intended for use with WebDriver, not Selenium RC. The keyDown() takes ""\" followed by the numeric keycode of the key to be pressed, normally the ASCII value of that key" as an argument. But 'keyPressNative()' takes "an integer keycode number corresponding to a java.awt.event.KeyEvent; note that Java keycodes are NOT the same thing as JavaScript keycodes!". Yes, it's confusing. One more reason to switch to Selenium 2 (WebDriver) :-).
6

In WebDriver there is a class keys. Using this we can send the ENTER key. Like this

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

For Selenium RC:

selenium.keyPress("elementid", "\\13"); // Note the double backslash

2 Comments

If poster uses WebDriver, the above code would work nicely. If Selenium RC is used you can use: selenium.keyPress("elementid", "\13");
shouldn't it be selenium.keyPress("elementid", "\\13");?
1

Have you tried this?

selenium.keyPressNative("\n");

1 Comment

Error for "\n": ERROR - Problem during keyDown: java.lang.NumberFormatException: For input string: " " at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:449) at java.lang.Integer.parseInt(Integer.java:499) at org.openqa.selenium.server.SeleniumDriverResourceHandler.doCommand(SeleniumDriverResourceHandler.java:518) ...
1

Try this. The application , which I am testing, requires a user to do the following steps for entering a value into a table cell. (click the cell, press enter key, enter a number, press enter again to complete) It is working for my script

selenium.click("id_locator");

this.command_waitInSeconds(1);

selenium.keyPress("id_locator", "\\13");

this.command_waitInSeconds(3);

selenium.type("name=value", "10000");

selenium.focus("name=value");

selenium.keyPress("name=value", "\\13");

Comments

1

If you are writing your selenese as HTML (or with the IDE), the ENTER key is available in a the variable ${KEY_ENTER}

Here is a example with sendKeys:

sendKeys | id=search | ${KEY_ENTER}

You can see a list of all available keys here: http://blog.reallysimplethoughts.com/2013/09/25/using-special-keys-in-selenium-ide-part-2/

Comments

0

Try to Use XPATH for searching the Element and then use the Following code. It worked for me.

 driver.findElement(By.xpath(".//*[@id='txtFilterContentUnit']")).sendKeys(Keys.ENTER);

Comments

0
webElem=driver.findElement(By.xpath(""));
webElem.sendKeys(Keys.TAB);
webElem.sendKeys(Keys.ENTER);

Comments

-2

Have you tried this ? (this is in ruby)

$driver.find_element(:id, "program").send_keys [:return]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.