tags:

views:

20

answers:

1

hi all.

I'm using selenium-client to test a site. I want to get all a tags matching a particular selector, and then get the href attributes back in an array. Can anyone tell me how i can do this? For example, this returns the href of the first matching 'a' tag:

browser.get_attribute "//a[contains(@id,'friendImageLink')]/@href"
=> "http://asite/some-path"

Can i do the above and end up with an array of href strings? It feels like it should be easy but i can't work it out :/ If i do this in firebug

$x("//a[contains(@id,'friendImageLink')]/@href")

I get an array of objects back, so it feels like i'm close.

thanks, max

EDIT - i thought of counting the matching elements and then iterating through them, but i can't seem to select the n'th element at all. For example, the page in question has 38 a tags matching the "contains" condition, but if i do this

browser.get_attribute "//a[contains(@id,'friendImageLink')][2]/@href"

I get an error message saying Selenium::CommandError: OR: Element /descendant::a[contains(@id,'friendImageLink')][2]/ not found

EDIT - i just figured out a way. @browser.get_all_links returns all the ids of all links on the page in an array. I can then grep out the ones i want and iterate over the resulting filtered array, getting each link and then getting it's href. eg

@browser.get_all_links.grep(/friendImageLink/).collect{|link_id| @browser.get_attribute("//a[@id='#{link_id}']/@href") }
+4  A: 

This is FAQ. This:

//a[contains(@id,'friendImageLink')][2]

Means: Any a being the second a child having an attribute id containing friendImageLink

// operator has more precedence than predicate

You need:

(//a[contains(@id,'friendImageLink')])[2]/@href
Alejandro
+1 for a correct explanation.
Dimitre Novatchev
aaahhhh...thanks.
Max Williams
@Max Williams: You are wellcome.
Alejandro