0

I am trying to find an element in a webpage based on the child of the child of the element I want. I need this because it is the clickable part of the webpage.

<table id="uniqueId">
  <tbody>
    <tr></tr>
    <tr class="row" onclick="do_a_thing">
      <td>
        <input type="hidden" value="1" />
      </td>
      <td>
        <input type="hidden" value="1" />
      </td>
    </tr>
    <tr class="row" onclick="do_a_thing">
      <td>
        <input type="hidden" value="2" />
      </td>
      <td>
        <input type="hidden" value="1" />
      </td>
    </tr>
    <tr class="row" onclick="do_a_thing">
      <td>
        <input type="hidden" value="3" />
      </td>
      <td>
        <input type="hidden" value="1" />
      </td>
    </tr>
  </tbody>
</table>

I currently have the following xpath to select the table row that contains the attribute where an input has value = 2:

//*table[@id='uniqueId']//tr[td[0]/value='2']

According to a previous question, this should work. Why doesn't it work here?

Community
  • 1
  • 1
bagelmakers
  • 384
  • 2
  • 15
  • 1
    When you say something doesn't "work", *please*, always tell what result you got (and preferably, what result you expected instead). Otherwise you leave people guessing as to whether you received an error message, or a wrong result, and what the result was. And it may be that the error is not in the result you received but in your expectation of what you should have received. – LarsH Feb 03 '15 at 21:16

1 Answers1

2

The following XPath

//table[@id='uniqueId']//tr[td/input[@value='2']]

when applied to your input HTML has the output

<tr class="row" onclick="do_a_thing">
  <td>
    <input type="hidden" value="2" />
  </td>
  <td>
    <input type="hidden" value="1" />
  </td>
</tr>

The XPath tr[td[0]/value='2'] won't work as the value attribute with the value 2 belongs to the input which is in the td:

tr[td/input[@value='2']]

and in addition, as mentioned by LarsH as comment, td[0] meaning td[position() = 0] won't select any element as position() is 1-based, so [0] is a predicate that is always false.

matthias_h
  • 11,356
  • 9
  • 22
  • 40
  • @bagelmakers Then it would return all rows as all `tr` contain a `td` with an `input` with value `1` – matthias_h Feb 03 '15 at 20:37
  • This works perfectly! I would just add `//table[@id='uniqueId']//tr[td[1]/input[@value='2']]` to be more specific. – bagelmakers Feb 03 '15 at 20:38
  • 1
    @bagelmakers Glad that helped you. And about the adjustment to target a specific `td` - I just wasn't sure if you always know that the `input value="2"` is in a specific `td` as you mentioned in your question that you want to select a row that contains an input with this value. – matthias_h Feb 03 '15 at 20:43
  • 2
    Just to add to this answer: another reason `tr[td[0]/value='2']` doesn't select any nodes is because `td[0]` means `td[position() = 0]`, but the value of `position()` is 1-based. So `[0]` is a predicate that is always false. – LarsH Feb 03 '15 at 21:19
  • @LarsH Thanks for mentioning, I've just added this to the answer as I was just focused on the XPath selecting the input by value when posting. – matthias_h Feb 03 '15 at 21:35