Select a row based on the contents of a cell with xpath
I have a table that consists of multiple rows that each contain 5 cells, like this:
<tr>
<td开发者_StackOverflow社区></td>
<td>123456</td>
<td>statusText</td>
<td><a>linkText</a></td>
<td>editButton</td>
</tr>
The 123456 could be any string of random letters and numbers. I want to be able to select a link based on the contents of the second cell in the table. I've been trying something like this:
//tr[contains(td, '123456')]
to get me to the cell, but it either returns every row or nothing, depending on how I tweak the xpath.
I've been trying something like this:
//tr[contains(td, '123456')]
to get me to the cell, but it either returns every row or nothing, depending on how I tweak the xpath
You get what you asked for. The above XPath expression selects any tr
element (row) in the document that has (at least one) td
child whose string value contains '123456'.
But you want:
//tr/td[text() = '123456']
this selects every td
element (cell) in the document, that has a text node child, whose string value is '123456'.
There can be different variations, depending on whether a td
may have more than one text nodes and on whether the white space in a text node should be normalized, but the question doesn't provide any information if any of these apply in this particular case.
I'd research something like //tr[string(td[2]) = '123456']
. If this does not work, I'd look up XPath axes.
精彩评论