Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath to select tr that has several specific values in the columns

Tags:

xpath

I have a table with multiple rows, and I'm trying to use an xpath to find the one row that contains specific values in each column. For example, I expect "foo" in column 1, "bar" in column 2, and "baz" in column 3.

What xpath could I use to get a row that contains those three specific values in those specific columns?

For example, given the following table I want to be able to get back only the one row with the three expected values in their expected columns

<table>
  <tr><td>foo</td><td>bar</td><td>xxx</td></tr>
  <tr><td>xxx</td><td>bar</td><td>baz</td></tr>
  <tr><td>foo</td><td>bar</td><td>baz</td></tr>
  <tr><td>bar</td><td>baz</td><td>foo</td></tr>
  <tr><td>foo</td><td>xxx</td><td>baz</td></tr>
</table>
like image 397
Bryan Oakley Avatar asked Dec 04 '22 16:12

Bryan Oakley


1 Answers

This does the trick (assuming the table element is the current context node):

    tr[(td[1] = 'foo') and (td[2] = 'bar') and (td[3] = 'baz')]

td[1] selects the first td child, td[2] the second, etc. Then you combine the conditions with and.

like image 138
csd Avatar answered Jan 01 '23 09:01

csd