Andrea Angeli Andrea Angeli - 7 months ago 8
HTML Question

Xpath selects just some tr

I have an html code like this

<table>
<tr>
<th><a href="example.html" title="exemple_title_th"></a><th>
<td></td>
<td></td>
<tr>
<tr>
<td><a href="example2.html" title="exemple_title_td"></a><td>
<td></td>
<td></td>
<tr>
<tr>
<td><a href="example3.html" title="exemple_title_td"></a><td>
<td></td>
<td></td>
<tr>
<tr>
<th><a href="example4.html" title="exemple_title_th"></a><th>
<td></td>
<td></td>
<tr>




And I want just the tr that don't have a th tag.
I tried this xpath:

//a[@title="exemple_title_th"]/following::tr


but it returns all tr after the first-one, including the following tr that contain the th tag that I don't want. What's wrong in this code?

Answer

Your xpath, //a[@title="exemple_title_th"]/following::tr, uses following axis, which is supposed to select all the following tr elements in the document order.

To select tr elements in the whole document that do not have a thchild, you can use a simple xpath like:

//tr[not(th)]
Comments