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

11,241

Solution 1

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.

Solution 2

This also does the trick...I'm sure there are faster ways to get what you want though, by not using xpath..

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

Share:
11,241
Russell Smith
Author by

Russell Smith

I started as a FORTRAN programmer, paid my dues writing C and X11/Motif, switched to Perl, discovered Tk, and from that, Tcl, and spent the next decade plus writing cross-platform GUIs in Tcl/Tk. I then spent three years using python and a smattering of ruby to create a cross-platform automated testing framework. After that I had two more gigs in windows shows, building automated testing frameworks and leading automation teams. I am now back to being a python developer at a small company in the independent publishing business. I was profiled as python developer of the week at http://www.blog.pythonlibrary.org/2015/03/02/pydev-of-the-week-bryan-oakley/ The open source projects I currently am active on are: The Robot Framework Hub - A web app and RESTful API for accessing robot framework test assets Robot Framework Lint - a static analysis tool for robot framework test files. Page Object Library - an implementation of the page object pattern for robot framework Robot framework extension for the brackets open source editor. I also maintain a sporadically-updated blog at boakley.github.io, focused mainly on my work with the robot framework.

Updated on June 25, 2022

Comments

  • Russell Smith
    Russell Smith about 2 years

    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>