tags:

views:

186

answers:

3

In selenium IDE, I need to find the 3rd link whose text is 'XXX'

<tr>
  <td>clickAndWait</td>
  <td>//a[text()='XXX'][3]</td>
  <td></td>
</tr>

error: element not found, any idea?

A: 

That expression works in my tests. What error or behavior are you seeing?

jwismar
I'm using selenium IDE, [error] Element //a[text()='XXX'][3] not found
I'm testing using XMLSpy, and an XHTML document with 3 <a> elements. It seems to work in my setup. Sorry, I haven't used Selenium, and don't have any insight into that aspect.
jwismar
Selenium is not known for having a good XPath engine...
0xA3
A: 

Use:

(//a[text()='XXX'])[3]

The expression:

//a[text()='XXX'][3]

selects every a element that has some text child with value 'XXX' and which is the 3rd child of its parent. Obviously, there are no such nodes, and you do not want this but you want the 3-rd from all such a elements.

This is exactly selected by the first XPath expression above.

Dimitre Novatchev
nope, same error as before.
A: 

As answered in my comment on http://stackoverflow.com/questions/2816261

It may be because of a subtlety in XPath where //a[1] will select all descendant a elements that are the first para children of their parents, and not the first a element in the entire document. It might work better for you to use something like //body/descendant::a[1] or anchor it to an element with an id like id('myLinks')/descendant::a[1]. Note that for the last example you would need to proceed the locator with xpath=.

Dave Hunt
I need to first filter by text though, not id,locator=text('myLinks')/descendant::a[1] is an invalid xpath, I need to find the nth link whose text is myLinks. The links can be anywhere in the document.
I didn't suggest using `locator=text('myLinks')/descendant::a[1]`, which is indeed an invalid XPath. Does my suggestion of `//body/descendant::a[1]` not work?
Dave Hunt