help required on autocomplete list by sendkeys

1,228 views
Skip to first unread message

mahadi

unread,
Sep 13, 2012, 1:10:23 PM9/13/12
to seleniu...@googlegroups.com
Hi,

We have a search text box, where based on user input auto-complete search result list is displaying. Now when i write the bellow code then webdriver can be able to write my string successfully on the text box, but it do not generates the auto-complete list which i need.

driver.findElement(By.cssSelector("#opGata")).sendKeys("Rinkeby");

The HTML is like on the image in this link: http://www.screencast.com/t/eEJiXC07

Can anyone please help me out.

Best Regards,
Mahadi

Peter Gale

unread,
Sep 13, 2012, 2:00:28 PM9/13/12
to Selenium Users
If you look at the events on the element you are sending text to, you'll see that the autocomplete seems to be triggered by a key up or key down keyboard action.

You can send these with something like: .sendKeys(KEYS.KEYUP)

(CI'm guessign the correct code but your IDE shoudl give you a list to choose from.)

That should trigger the autocomplete.



Date: Thu, 13 Sep 2012 10:10:23 -0700
From: maha...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] help required on autocomplete list by sendkeys
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/pAyjtDVG41oJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mahadi

unread,
Sep 13, 2012, 3:46:22 PM9/13/12
to seleniu...@googlegroups.com
Hi Peter,

I am not sure if i get you properly. I tried bellow but this do not helps me:

driver.findElement(By.cssSelector("#opGata")).sendKeys("R");
driver.findElement(By.cssSelector("#opGata")).sendKeys(Keys.DOWN);
driver.findElement(By.cssSelector("#opGata")).sendKeys("i");
driver.findElement(By.cssSelector("#opGata")).sendKeys(Keys.DOWN);
driver.findElement(By.cssSelector("#opGata")).sendKeys("n");
driver.findElement(By.cssSelector("#opGata")).sendKeys(Keys.DOWN);
driver.findElement(By.cssSelector("#opGata")).sendKeys("k");
driver.findElement(By.cssSelector("#opGata")).sendKeys(Keys.DOWN);
driver.findElement(By.cssSelector("#opGata")).sendKeys("e");
driver.findElement(By.cssSelector("#opGata")).sendKeys(Keys.DOWN);
driver.findElement(By.cssSelector("#opGata")).sendKeys("b");
driver.findElement(By.cssSelector("#opGata")).sendKeys(Keys.DOWN);
driver.findElement(By.cssSelector("#opGata")).sendKeys("y");
driver.findElement(By.cssSelector("#opGata")).sendKeys(Keys.DOWN);


Best Regards,
Mahadi

Peter Gale

unread,
Sep 13, 2012, 3:53:20 PM9/13/12
to Selenium Users
That's what I was suggesting. Not sure why it's not working.

You could use javascript to execute the onkeysup/down event on the element after each keypress, or it may be that they're not the events you actually need too trigger. Or it may have something to do with the "autocomplete" attribute being set to "off".

I'd suggest speaking to the develops to ask what specific action you should take to trigger their autocomplete routine, showing the example below for the type of thing that you can do.


Date: Thu, 13 Sep 2012 12:46:22 -0700
From: maha...@gmail.com
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] help required on autocomplete list by sendkeys
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/72YFIvpu6pcJ.

Vladislav Mkrtychev

unread,
Sep 13, 2012, 4:07:49 PM9/13/12
to seleniu...@googlegroups.com
Hello,

I don't know if it will help, but I also had similar issue with auto complete.
I am still working with IDE so I am not sure if my solution will apply.
Here is what I did...

<tr>
<td>type</td>
<td>id=Technician_1</td>
<td>Se</td>
</tr>
<tr>
<td>typeKeys</td>
<td>id=Technician_1</td>
<td>Se</td>
</tr>
<tr>
<td>mouseOver</td>
<td>//html/body/ul[2]/li[2]/a</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>//html/body/ul[2]/li[2]/a</td>
<td></td>
</tr>


So there are 4 events that take place:
Type
TypeKeys
MouseOver
Click

Obviously, assumption here is, there is predefined set of name (values) and they are always in the same order as far as auto complete goes. 

Thanks!
Vladislav Mkrtychev

Ross Patterson

unread,
Sep 13, 2012, 4:35:10 PM9/13/12
to seleniu...@googlegroups.com

Keys.DOWN is a synonym for Keys.ARROW_DOWN (likewise UP, LEFT, and RIGHT), which is most likely not what you want.  An individual-character call to sendKeys() ought to trigger the sending of a single keystroke, as I understand the WebElement  interface.  Of course, the code you’ve written types them as fast as it can, so maybe you’re not allowing the autocomplete the time it needs to trigger.

 

Ross

Peter Gale

unread,
Sep 13, 2012, 4:48:59 PM9/13/12
to Selenium Users
Thanks, Ross.

Yes I meant to recommend sending a sequence which registers with the browser as the action of pressing any key down, or releasing it, not sending an up/down arrow.

I can't see anything under the KEYS class that does that.

Unless anyone can suggest otherwise, initiating the event via javascript might be the best option.


From: rpatt...@parature.com
To: seleniu...@googlegroups.com
Date: Thu, 13 Sep 2012 15:35:10 -0500
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.

mahadi

unread,
Sep 14, 2012, 4:08:51 AM9/14/12
to seleniu...@googlegroups.com
Thanks every body for valuable suggestions.

@PeterJef: I am so sorry that your suggestion was working fine, it was not working at that time because of a bug at that time in our test environment :). Finally i managed to make it workable simply with the bellow two lines of code:

driver.findElement(By.cssSelector("#opGata")).sendKeys("Rinkeby");
driver.findElement(By.cssSelector("#opGata")).sendKeys(Keys.DOWN);

@Ross: Is this is the standard way to trigger the auto-complete list, or you have some good way recommendation to do this?

Best Regards,
Mahadi

Ross Patterson

unread,
Sep 14, 2012, 8:37:00 AM9/14/12
to seleniu...@googlegroups.com

That code will do exactly what it says: type the keystrokes “R”, “i”, “n”, “k”, “e”, “b”, “y”, down_arrrow as fast as it can.  If your application responds to that” by displaying a list of choices for “Rinkeby” and selecting the topmost item, then yes, that will do what you want.

 

There is no standard way to trigger an auto-complete list, because there is no standard auto-complete list (unless you’re talking about HTML5’s “autocomplete=’on’” attribute).  If it works, use it, if not, you’ll need to figure out what works for whatever auto-complete JavaScript library your application is using.

 

Ross

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of mahadi
Sent: Friday, September 14, 2012 4:09 AM
To: seleniu...@googlegroups.com
Subject: Re: [selenium-users] help required on autocomplete list by sendkeys

 

Thanks every body for valuable suggestions.



@PeterJef: I am so sorry that your suggestion was working fine, it was not working at that time because of a bug at that time in our test environment :). Finally i managed to make it workable simply with the bellow two lines of code:

Ross Patterson

unread,
Sep 14, 2012, 8:46:57 AM9/14/12
to seleniu...@googlegroups.com

The sendKeys() method will “press” and “release” each of the keys specified, in order.  If you want to combine multiple keypresses, I think you have to use the Actions interface for that (although I don’t really know how that works).  I believe you can use that for things like CTRL+<some key>, etc.

 

Nice to see Mahadi’s separate post indicating the sendKeys(“Ringeby”) was indeed the correct answer, and that there was a non-Selenium problem that prevented it from working at first J

 

Ross

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Peter Gale
Sent: Thursday, September 13, 2012 4:49 PM
To: Selenium Users
Subject: RE: [selenium-users] help required on autocomplete list by sendkeys

 

Thanks, Ross.

Peter Gale

unread,
Sep 14, 2012, 8:56:46 AM9/14/12
to Selenium Users
That's for that update and explanation, Ross.

Glad to know that the press/release actions aren't omitted - they're just inlcuded for free!

I use sendKeys(Keys.CONTROL+"v"), for example withouth having to use Actions classes, though maybe the actions classes are required for some more complex/obscure combinations.

Date: Fri, 14 Sep 2012 07:46:57 -0500

Subject: RE: [selenium-users] help required on autocomplete list by sendkeys

The sendKeys() method will “press” and “release” each of the keys specified, in order.  If you want to combine multiple keypresses, I think you have to use the Actions interface for that (although I don’t really know how that works).  I believe you can use that for things like CTRL+<some key>, etc.

 

Nice to see Mahadi’s separate post indicating the sendKeys(“Ringeby”) was indeed the correct answer, and that there was a non-Selenium problem that prevented it from working at first J

 

Ross

 

From: seleniu...@googlegroups.com [mailto:seleniu...@googlegroups.com] On Behalf Of Peter Gale
Sent: Thursday, September 13, 2012 4:49 PM
To: Selenium Users
Subject: RE: [selenium-users] help required on autocomplete list by sendkeys

 

Thanks, Ross.

Yes I meant to recommend sending a sequence which registers with the browser as the action of pressing any key down, or releasing it, not sending an up/down arrow.

I can't see anything under the KEYS class that does that.

Unless anyone can suggest otherwise, initiating the event via javascript might be the best option.


Reply all
Reply to author
Forward
0 new messages