Imagine I am given a table like this:
<table>
<tr><td>A</td></tr>
<tr><td><a href="#">B</a></td></tr>
<tr><td><a href="#">C</a></td></tr>
<tr><td>D</td></tr>
<tr><td><a href="#">E</a></td></tr>
<tr><td>B</td></tr>
</table>
I'd like to construct a CSS selector (preferred) or an XPath (accepted) that picks out the n'th row that contains an a
anchor, such that:
selector(1) => <a href="#">B</a>
selector(2) => <a href="#">C</a>
selector(3) => <a href="#">E</a>
CSS selectors
At this point, I'm pretty sure that CSS won't do the job, but
'table tr:nth-child(' + n + ')'
will pick out the n'th row, but that selects rows whether or not they have an a
anchor. Similarly,
'table tr:nth-child(' + n + ') a'
will pick out rows with an a
anchor, but only if n
is 2, 3 or 5.
XPath
With XPath, this matches all the tr
that have an a
`//table//tr//a/ancestor::tr`
but I can't figure out how to select the n'th match. In particular,
`//table//tr//a/ancestor::tr[position() = 2]`
doesn't appear to select anything.