What does () contain in XPath?

I have two almost identical tables, the only difference is the input tag in the first:

Table 1

  <table>
    <tbody>
      <tr>
        <td>
          <div>
            <input type="text" name="" value=""/>
          </div>
        </td>
      </tr>
    </tbody>
  </table>

Table 2

  <table>
    <tbody>
      <tr>
        <td>
          <div></div>
        </td>
      </tr>
    </tbody>
  </table>
</body>

When I use this XPath //table//tbody//tr[position()=1 and contains(.,input)], it returns both the 1st row table, not just the 1st table of the 1st row, as I expect.

However, this XPath //table//tbody//tr[position()=1]//inputonly returns inputin the first.

So what am I doing wrong? Why is the same inputassociated with both tables? Am I misusing .here?

+4
source share
2 answers

Due to a poor choice in function names 1, many people mistakenly accept a goal in XPath: contains()

  • XPath contains()checks not an element containment.
  • XPath contains() .

, tr[contains(.,input)] , , , . tr, , input; . . (, true, .) , .

, , .//input. tr, XPath , tr , ,

//table//tbody//tr[position()=1 and .//input]

table ( @Andersson), table , , input :

//table[.//input]

XPath contains() string-contains()

1 XML, , , . 24 XPath, 19 node ; 5 . , contains(). XPath contains() string-contains().

+7

//table[.//input]

table node, input

+3

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


All Articles