1

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?

2 Answers 2

0

As answered in my comment on selenium scripts

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=.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.