how to access an element from a multi level nested div/ul/li

200 views
Skip to first unread message

a b

unread,
Mar 31, 2011, 11:35:07 AM3/31/11
to Watir General
Hi,

please tell me the email address where I can send an attached file.

Thanks,
Cristina

Adam Reed

unread,
Mar 31, 2011, 6:23:26 PM3/31/11
to Watir General
Cristina, you can copy/paste the relevant html here directly (with
care to remove superfluous elements). If the div has an attribute
that you can access (class, name, id), the nested list elements may
not enter into it:

browser.div(:how, "what").text will grab all of the text in this
div. Example: browser.div(:class, "my-div-element").text.

If you need a more specific locator, you will probably need to use
XPath (http://wiki.openqa.org/display/WTR/XPath) to locate the item.

Thanks,
Adam

joedio

unread,
Apr 1, 2011, 1:10:50 PM4/1/11
to Watir General
When I'm working with elements within a nested element, I collect the
number of that elements (e.g. table) present on the page, then loop
backwards through them starting with the deepest nested element
(the one with the highest index number), and
start looking for the element I'm interested in there, if its not
found continue to the next lowest element's index number.

This assures that the element being searched for will be found within
the
nested element that is its direct parent.

This technique work for nested tables, frames, spans, divs, etc.

Hope this helps,

Joe

Chuck van der Linden

unread,
Apr 4, 2011, 10:40:58 AM4/4/11
to Watir General
The only time that 'nesting' really matters is

1) items inside a frame, which must always be addressed starting at
the frame level (but you do not need to specify the 'path' to the item
unless that's the only way to identify it)

2) The item you want lacks a means to uniquely identify it (e.g. no
name, id, class, unique text, etc)

As long as there is some way (via a single value such as ID, or a
combination such as Class and Name) to uniquely identify an object,
you don't need to worry about how deeply it is nested.

If the element you want does not have any unique characteristics, then
it's time to start looking at the containers that the item is
'wrapped' by, and see if one of them can be incorporated to create a
'key' of sorts that will let you address the object..

To get more specific, and give you good advice, you REALLY need to
post some sample HTML that shows the section of the page around the
element you are trying to address, (along with telling us WHICH
element you are trying to address) and then people can give you
specific examples of Watir code that could be used to work with that
element
Reply all
Reply to author
Forward
0 new messages