Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select node, which contains descendants with specific attribute

Tags:

xpath

I have similiar structure to:

<tr>
    <td>I WANT THIS</td>
    <td>
        <a class="unread">text</a>
    </td>
    <td></td>
</tr>
<tr>
    <td></td>
    <td>
        <a class="read">text</a>
    </td>
    <td></td>
</tr>

And I need to select <tr> node, which have <a> node with attribute [@class='unread'], to select inner <td> later.

I tried //tr[a/@class='unread'] and //tr/a[@class='textMsg unread'] but didn't work. How can I get my <tr> node?

like image 605
Sk1X1 Avatar asked Apr 10 '13 00:04

Sk1X1


2 Answers

a tag is not a child of tr tag, you can try this xpath:

//tr[.//a/@class='unread']

Or

//tr[descendant::a/@class='unread']
like image 113
kev Avatar answered Sep 17 '22 22:09

kev


To select the wanted td element(s), use:

//tr//td[.//a[@class = 'unread']]

If it is known that the td is a child of the tr and the a is a child of the td, the above may be simplified to:

//tr/td[a[@class = 'unread']]
like image 21
Dimitre Novatchev Avatar answered Sep 20 '22 22:09

Dimitre Novatchev