Accessing an unordered list

309 views
Skip to first unread message

Anne

unread,
Apr 6, 2011, 4:49:20 PM4/6/11
to Watir General
I've been doing automation for a while but am new to Ruby/Watir. I'm
trying to access the first item in an unordered list and everything I
try returns an Argument Error ("wrong number of arguments (2 for
0)"). I've been hitting my head against a wall for a day now and just
can't seem to get it. Can you please help?

the html looks like this:

<div id="searchResults">
<div id="priorities">
<div id="resultList">
<ul>
<li>
<a id="abc/123" class"searchResult
searchResultPreviewed" href="/mystuff/my-topic"> My Topic Title
</a>
</li>
+<li>
+<li>
</ul>

I'm trying to click on the first <li> item (which is one of many).
I've tried:
browser.lis(:id, "abc/123").click
browser.lis(:xpath, "//a[id='abc/123']/").click
browser.lis(:text, "My Topic Title").click
I also tried all these with .select instead of .click

each time I've gotten
ArgumentError: wrong number of arguments (2 for 0)

I know I'm missing something basic. Can you point me in the right
direction?

Thanks!
Anne

Keith Hughes

unread,
Apr 6, 2011, 6:35:42 PM4/6/11
to watir-...@googlegroups.com
Hi Anne,
 
Have you tried   browser.li(:id=> "abc/123",:index=>1).click   ?
 
rgds
Keith



Adam Reed

unread,
Apr 7, 2011, 11:00:38 AM4/7/11
to Watir General
Hi Anne,

The reason you're getting that error is that you're using "lis" rather
than "li" in your code. This is like saying browser.images, as in
browser.images.each do |img|... To reach a singular element, you want
to use the singular locator.

Also, while you are certainly able to click on an "li" it looks like
the element you're actually trying to access is a link (denoted by the
anchor tag and href element: <a href="">). Unless there are multiple
links on the page that have the exact same descriptors as this one,
there is no reason to access the link's parent element.

You may want to try this instead:

browser.link(:href, "/mystuff/my-topic").click
browser.link(:text, "My Topic Title").click

Thanks,
Adam

On Apr 6, 5:35 pm, Keith Hughes <khughe...@gmail.com> wrote:
> Hi Anne,
>
> Have you tried   browser.li(:id=> "abc/123",:index=>1).click   ?
>
> rgds
> Keith
>
> > Before posting, please readhttp://watir.com/support. In short: search

Chuck van der Linden

unread,
Apr 7, 2011, 11:04:19 AM4/7/11
to Watir General
The ID 'abc/123' belongs to a element of type link.. to click on it

browser.link(:id, "abc/123").click

Assuming you did not know the ID of that item, and wanted to always
click the first list item inside the resultList, then you could do

browser.div(:id, 'resultList').li(:index, 1).click

However, unless the list item element has some kind of onclick event
setup, clicking the 'list item' itself might not do much, you may need
to be clicking the link inside it

browser.div(:id, 'resultList').li(:index, 1).link(:index, 1).click

That will click the first link, inside the first listitem, inside the
'resultList' div. Or to express it in the order the code is written:
'using the div with id abc/123, find the first list item inside that
div, and the first link inside that list-item then click on it.

Anne

unread,
Apr 7, 2011, 11:14:05 AM4/7/11
to Watir General
Thanks, everyone, for all the great info. I'll try your suggestions
and
let you know how it goes.

Anne

unread,
Apr 7, 2011, 11:24:58 AM4/7/11
to Watir General
Thanks to all of you... it's working...

If I ever get a chance to meet you guys at a conference, I owe you
each a drink...
or a giant chocolate chip cookie...

marko

unread,
Apr 7, 2011, 4:00:48 AM4/7/11
to Watir General
Hi Anne,

what Keith said.

The methods with the plural form (lis, images, divs etc.) return an
array of elements and you cannot click on an array.

You need to find a specific element and then click on it as Keith
suggested with the li method in this case.
Alternatively you can use the plural form and then narrow the search
down later on, for example

browser.lis.first.click (if you wanted to click on the first item)

Br.
Marko
Reply all
Reply to author
Forward
0 new messages