Nimish Tripathi
Nimish Tripathi

Reputation: 21

sendKeys.RETURN not working in FirefoxDriver

if I execute the following code in FireFoxDriver:

WebElement element = driver.findElements(By.id("some_id")); // element being a textbox
element.sendKeys("apple"); 
element.sendKeys(Keys.RETURN); 

The sendKeys(Keys.RETURN) is not performing its desired function. Actually what I am trying to do is Input a text in a dynamic text search box (like one in facebook search) and press enter. The input is working fine but not the enter key.

sendKeys("apple") works, even sendKeys(Keys.BACK_SPACE) works, but not Keys.RETURN.

Does anyone have ideas? Thanks guys!

Upvotes: 1

Views: 5382

Answers (3)

mynameistechno
mynameistechno

Reputation: 3543

Not exactly sure why this happens, but there are a couple alternate ways of doing this that may help:

If elements are in a form, and there is no javascript that runs on submit or something you can use .submit() on any form input element, such as inputs and textareas:

WebElement element = driver.findElements(By.id("some_id"));
element.sendKeys("apple");
element.submit()

You can send the newline character with your input:

WebElement element = driver.findElements(By.id("some_id"));
element.sendKeys("apple\n");

Provide send_keys a list:

WebElement element = driver.findElements(By.id("some_id"));
element.sendKeys("apple", Keys.ENTER);

Upvotes: 2

user2496135
user2496135

Reputation: 1

I tried sending \n and fiddled with various commands until I found someone explaining that "keyPress (target) 13" will send the return key.

So first I use type to enter the string I want ...

*

*<tr>
    <td>type</td>
    <td>id=status</td>
    <td>This is my test string</td>
</tr>*

*

... and then send the Enter key to the same text input box

*

*<tr>
    <td>keyPress</td>
    <td>id=status</td>
    <td>13</td>
</tr>*

*

Upvotes: 0

Nimish Tripathi
Nimish Tripathi

Reputation: 21

Got the solution to the above problem. U just need to add, a delay. This happens because the Java Class runs too fast, so if u have sent a call, and pressed enter/ tab, before the element arrives, the enter is pressed, that is why this doesn't work. Just add Thread.delay(1000); before your Keys.RETURN command. That will do. Worked for me.

Upvotes: 1

Related Questions