Cathal
Cathal

Reputation: 1338

Selenium sendKeys(string) method only sending part of string

I use the sendKeys method to send a string to a search box. The problem is that only the first couple (it differs) of keys are being sent. With the result that the search box is not able to filter content correctly. Below is a snippet of my code:

String currLab = labsInCloud.get(j); //get a lab name from a list
evtFilter_fld.clear(); //clear the filter box
evtFilter_fld.sendKeys(currLab); //send keys to filter box
WebElement selectLab = getDriver().findElement(mainPage_selectLab_i(1)); //select first item from        
                                                                         //filter

so for example if currLab = "test lab" only the "te" of currLab is sent to the filter box.

EDIT: Just to add that selectLab is selecting the incorrect item due to the full text not being sent.

Upvotes: 4

Views: 6809

Answers (3)

tormodatt
tormodatt

Reputation: 86

I had the same problem. Sometimes only a char in a 3-char string would be put in before it clicked the button.

I added the following workaround:

'blah'.split('').forEach((c) => element.sendKeys(c))

It is a bit slower than inputing all at once, but faster than a constant timeout, and it works.

Found the tip at https://github.com/angular/protractor/issues/1511 (it is closed since it is not a protractor issue)

You can also try the javascript approach

String theText = "asdf"    
((JavascriptExecutor) driver).executeScript("arguments[0].value='" + theText + "';", fieldElement);

Upvotes: 1

Aaron Silverman
Aaron Silverman

Reputation: 22635

I encountered this issue and it was a problem with my keyboard mapping.

My particular case was when running tests in a desktop environment over VNC. I was using tightvncserver and it was loading keyboard incorrectly. Switching to vnc4server resolved the problem.

Upvotes: 2

Cathal
Cathal

Reputation: 1338

It looks like i wasnt giving the driver enough time to send the keys. I added a sleep(1000) before attempting to select and item. Working fine now.

Upvotes: 0

Related Questions