What XPath selects the next <tr> after a <tr> containing X?

48,799

Solution 1

ChaosPandion's and Martin v. Löwis's answers both work for the sample you give, but if you are asking for the next tr, then presumably in some cases there are further tr elements in same table. In which case, the answers will give all the following or following-sibling tr elements.

Also going by the headline question rather than the sample, the xpath should probably allow for the X being in a th cell instead of td. And I'm guessing that you'd only want the following tr if it is in the same parent (thead, tbody, tfoot).

So I'd go for

//tr[* = 'X']/following-sibling::tr[1]

Solution 2

This should work.

tr[td/text() = 'X']/following-sibling::node()

Solution 3

//td[.='X']/following::tr
Share:
48,799
Gordon
Author by

Gordon

I am a human with a name.

Updated on December 31, 2021

Comments

  • Gordon
    Gordon over 2 years
    <tr>
     <td>Blah!</td>
     <td>X</td> <!-- TR containing X -->
     <td>Woot!</td>
    </tr>
    <tr>
     <td>Useful Data, contents unknown</td> <!-- Select this TR -->
    </tr>
    <tr>
     <td>Useless data</td> <!-- Don't select this or any subsequent TR -->
    </tr>
    <tr>
     <td>More crap I don't want</td>
    </tr>
    <tr>
     <td>X</td> <!-- Another X -->
    </tr>
    <tr>
     <td>Useful</td> <!-- Do select this one, since previous has X -->
    </tr>
    

    What XPath would return the <tr> immediately following the <tr> that contains X?