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

XhtmlXpath

Xhtml Problem Overview


<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?

Xhtml Solutions


Solution 1 - Xhtml

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 - Xhtml

This should work.

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

Solution 3 - Xhtml

//td[.='X']/following::tr

Solution 4 - Xhtml

Use this for finding the next element from your element. Keep increasing for every hop:

//android.widget.TextView[contains(@text,'US Dollar')]/following::android.widget.TextView[1]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGordonView Question on Stackoverflow
Solution 1 - XhtmlAlohciView Answer on Stackoverflow
Solution 2 - XhtmlChaosPandionView Answer on Stackoverflow
Solution 3 - XhtmlMartin v. LöwisView Answer on Stackoverflow
Solution 4 - XhtmlHarsh TiwariView Answer on Stackoverflow