user1065345
user1065345

Reputation: 11

Xpath reference for addressing array of elements in Selenium

I have 4 span elements with the same properties in a page. I tried to click on the fourth span with the below code:

selenium.click("css=span:contains('item12_21'):nth-child(4)");

But I am getting the error "element not found". I am trying to click the element with different workarounds, but no luck. I even tried to give the entire hierarchy, but I have a node called #document in between the tags. Please let me know how to handle the #document in Xpath.

I have IE 7 and the application is not compatible with Firefox, so I'm working with the IE developer toolbar. Can any one please help me to solve the issue.

Upvotes: 0

Views: 3164

Answers (1)

jro
jro

Reputation: 9484

The basic XPath will look like this:

//span[contains(., 'item12_21')]

Then (I'm not fully sure which one you want), you can get either the fourth item from that filtered list:

(//span[contains(., 'item12_21')])[4]

... or every item that is the fourth descendant like this:

//span[contains(., 'item12_21')][4]

The brackets indicate the precedence for the indexing.

I'm hoping the #document item will be bypassed using this, but if it isn't, you'll need to post some of your html to show the context of your items.

Upvotes: 1

Related Questions