rakakot
rakakot

Reputation: 33

How to get position with XPath?

Let's say I have this HTML structure:

<table> 
  <tr>...</tr>  
  <tr>...</tr>  
  <tr> 
    <td>111</td> 
  </tr>  
  <tr> 
    <td>222</td> 
  </tr> 
</table>

What will be right XPath if I need to find number of <tr> tags from first <tr> to <tr> tag which has /td[text()='111']

In my example number of <tr> tags will be 3.

And result will be 4 if we want to see number of <tr> from beginning to <tr> which has "/td[@text='222']"

Upvotes: 2

Views: 224

Answers (1)

kjhughes
kjhughes

Reputation: 111541

This XPath

count(//tr[td ='111']/preceding-sibling::tr) + 1

applied against this markup

<table> 
  <tr>...</tr>  
  <tr>...</tr>  
  <tr> 
    <td>111</td> 
  </tr>  
  <tr> 
    <td>222</td> 
  </tr> 
</table>

will return

3

as requested.

Upvotes: 2

Related Questions