Find td with a specific value using xpath

Consider the following html fragment:

<table>
  <tr>
    <td>One</td><td>1</td>
    <td>Two</td><td>2</td>
  </tr>
</table>

I want to use xpath to search for the second td ("1" or "2") based on the value of the first td ("One" or "Two"). Sort of:

/table/tr/td[text() = 'One']/../td

or

/table/tr/td[text(), 'One']/../td

Any ideas?

+3
source share
2 answers

/table/tr/td[text()='One']/following-sibling::td[1]

"First tdnext next: tdnode with text One"

+14
source

next-related :: td?

+1
source

Source: https://habr.com/ru/post/1732602/


All Articles