2
<div id="suggestionlist">
<ol id="suggestionroot">
<li id="sugg_1">
<li id="sugg_2">
<li id="sugg_3">
<li id="sugg_4">
<li id="sugg_5">
<li id="sugg_6">
<li id="sugg_7">
<li id="sugg_8">
<li id="sugg_9">
<li id="sugg_10">

I have a search look ahead feature which I'm trying to automate. I'm trying to pick the 6th option in the list every time but I just can't seem to locate it! This is the nearest I've got but it's not working..

@Browser.div(:id, "suggestionlist").link(:index, 6).click
Holger Just
  • 52,918
  • 14
  • 115
  • 123

2 Answers2

1

You should do some reading about HTML. <li> tag is not link, <a> tag is link.

So, to click <li id="sugg_6"> try this:

browser.li(:id => "sugg_6").click

To click a link inside the list item (not shown in your HTML but referenced in comments)

browser.li(:id => "sugg_6").link.click

(that presumes you want to click the first/only link inside the LI, otherwise you might need to specify an index value)

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
  • *not working* is not enough data. What did or did not happen? What did you expect to happen? Maybe all you need to do is fire a javascript event: http://stackoverflow.com/questions/3787555/how-to-find-out-which-javascript-events-fired – Željko Filipin Nov 10 '11 at 09:07
  • I've fired the event: @Browser.text_field(:name, "searchTerms").fire_event('onkeydown') – Lisa Leanne Crowe Nov 10 '11 at 09:15
  • It just skips past that line and continues with the rest of the test, I want it to click the 6th suggestion – Lisa Leanne Crowe Nov 10 '11 at 09:16
  • Maybe you need to fire some other event. Take a look at the question I have linked in my previous comment. Can you talk to a developer of the site? They will probably know what needs to happen to click the list item. – Željko Filipin Nov 10 '11 at 09:28
  • Sorted it! @Browser.div(:id, "suggestionlist").li(:index, 7).link(:index, 1).click – Lisa Leanne Crowe Nov 10 '11 at 09:44
  • If your LI items have unique (and predictable) ID values you can skip specifying the div that contains them. If you are using Watir 2.x or Watir-webdriver, you don't need to specify the index of the link if what you want is the first one within the list item. – Chuck van der Linden Nov 10 '11 at 20:19
0

Did you try to access this element by XPath?

browser.find_elements_by_xpath("div[@id='suggestionlist'/li[6]").click

Invy
  • 1,058
  • 1
  • 10
  • 12