Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xhtml

xpath

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

like image 648
Gordon Avatar asked Aug 23 '09 03:08

Gordon


People also ask

How do I select the second element in XPath?

//div[@class='content'][2] means: Select all elements called div from anywhere in the document, but only the ones that have a class attribute whose value is equal to "content". Of those selected nodes, only keep those which are the second div[@class = 'content'] element of their parent.

Which XPath selects the parent of the current element?

import.io - Xpath to select next parent of the current node - Stack Overflow.

What is TD and TR in XPath?

The < tr > tag in the table indicates the rows in the table and that tag is used to get information about the number of rows in it. Number of columns of the web table in Selenium are calculated using XPath (//*[@id='customers']/tbody/tr[2]/td).

What is XPath selector?

XPath stands for XML Path Language. It uses a non-XML syntax to provide a flexible way of addressing (pointing to) different parts of an XML document. It can also be used to test addressed nodes within a document to determine whether they match a pattern or not.


2 Answers

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] 
like image 51
Alohci Avatar answered Sep 20 '22 10:09

Alohci


This should work.

tr[td/text() = 'X']/following-sibling::node() 
like image 43
ChaosPandion Avatar answered Sep 18 '22 10:09

ChaosPandion