views:

333

answers:

1

In selenium IDE, I need to click on the nth link that has text 'XXX'. How can this be done?

<tr>
<td>clickAndWait</td>
<td>//a[text()='XXX'][${link}]</td>
<td></td>
</tr>

The above code says [error] Element //a[text()='XXX'][9] not found even though it's valid,

A: 

For unstructured links, you might try something like //body/descendant::a[text()='a link'][9]. The descendant axis will flatten all the descendant 'a' tags so position() will take the order they appear in the document rather than being based on the previous siblings of each a tag.

If the links are semi-structured as below, then something like

//div[@id='fu']//a[text()='a link']/../following-sibling::*//a[text()='a link']/../following-sibling::*//a[text()='a link']/../following-sibling::*//a[text()='a link']/../following-sibling::*//a[text()='a link']/../following-sibling::*//a[text()='a link']/../following-sibling::*//a[text()='a link']/../following-sibling::*//a[text()='a link']/../following-sibling::*//a[text()='a link']

can find you the 9th matching link with the given structure.

<p>a segment, outside the containing div, with <a href="www.google.com">a link</a>.</p>

<div id="fu">

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a different link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<p>some text <a href="www.google.com">a link</a>.</p>

<div></div></div>

Leif Carlsen
what if it's not structure, it's all under a div, I would like to iterate through using index
You can probably use the descendant axis. I've updated my answer to include an example use.
Leif Carlsen