Using get_attribute() to retrieve multiple XPath locator attributes

234 views
Skip to first unread message

metametagirl

unread,
Apr 28, 2009, 9:00:22 PM4/28/09
to selenium-users...@googlegroups.com
Hi,
I'm using Selenium RC with Python and Perl.  I'd like to extract an 'href' attribute from multiple nodes, but when I use get_attribute(), I only get the 'href' attribute of the first node found.

For example, the locator I am using is:

     >>> locator = '//div[@class="info"]/p/a'

This locator evaluates to 40 nodes (this is the expected value) in my XPather Browser.

Also, I can use Selenium to extract this same count:

     >>> sel.get_xpath_count(locator)
     u'40'
My attribute locator is the same as the locator, with the 'href' attribute:\\      >>> attribute_locator = '//div[@class="info"]/p/a@href' However, when I try to use get_attribute(), I would like to get all 40 href's, but I only get the first one: \\      >>> sel.get_attribute(attribute_locator)     u'p_10153_12605_04647333000P?vName=Appliances&cName=Refrigerators&sName=Bottom+Freezers'
Can someone suggest an alternate XPath to get the result I want, or an alternate Selenium API method would work?I do have a functioning workaround, which uses indexes into the XPath nodes to get all 40 href's, though I find this unwieldy and error-prone, and it seems to defeat the purpose of using XPath in the first place. Any help is appreciated,metametagirl

metametagirl

unread,
Apr 29, 2009, 2:12:59 PM4/29/09
to selenium-users...@googlegroups.com
Hi David,
Thanks for your suggestion.  I'll give it a try and let you know how it goes.

metametagirl
(mmg -- I like that {emoticon:__jive_emoticon_name=happy})

metametagirl

unread,
Apr 29, 2009, 3:30:09 PM4/29/09
to selenium-users...@googlegroups.com
Using get_attribute_from_all_windows() either doesn't work the way I want it to, or I'm still missing something.

I'm able to use it to extract single string attributes from the DOM, i.e.:

     >>> sel.get_attribute_from_all_windows("document.title")
     [u'Bottom Freezer Refrigerators & French Door Refrigerators from Sears.com']
     >>> sel.get_attribute_from_all_windows("document.bgColor")     [u'#ffffff']
However, attempting to extract multiple elements just returns a list containing a string:

     >>> sel.get_attribute_from_all_windows('document.getElementsByTagName("div")')
     [u'[object HTMLCollection]']

Not sure if there is some issue here converting the resulting Java array to a Python list, or if I am still just not using this API correctly?

thanks,
metametagirl

metametagirl

unread,
Apr 30, 2009, 9:34:15 PM4/30/09
to selenium-users...@googlegroups.com
Hi David,

Thanks again for your suggestion.  Unfortunately, there isn't anything to iterate over, since when I try to do something like "get all the DIV elements", I just get a list containing a single string.  Presumably, this HTMLCollection object would contain all the DIV tag elements in Java, but in Python, all I get is the string "object HTMLCollection", not the actual object.

>>> for attribute in sel.get_attribute_from_all_windows('document.getElementsByTagName("div")'):
...     print attribute
...
[object HTMLCollection]
I'm able to retrieve single Javascript fields, such as the background color: >>> for attribute in sel.get_attribute_from_all_windows('document.bgColor'):...     print attribute... #ffffff But whenever multiple attribute results exist, I just get the "[object HTMLCollection]" result. Thanks for your help,metametagirl

Reply all
Reply to author
Forward
0 new messages