如何使用XPath包含()用于特定文本? [英] How to use XPath contains() for specific text?
问题描述
2 | 1 | 28 | 9 |
3 | 8 | 5 | 10 |
18 | 9 | 8 | 0 |
我想选择只包含8个单元格的单元格,也就是只有第2个单元格row3的row2和3rd单元格。
这是我试过的: // table // td [contains(。,'8')]
。它给了我所有包含8的单元格。所以,我也得到了不需要的值28和18。
我如何解决这个问题?
编辑:如果您想尝试xpath,这里是一个示例表。使用左侧的日历 - https://sfbay.craigslist.org/sfc/ 请仔细阅读 contains()
中的 功能。
使用它来测试 元素是否包含值是常见的错误 STRONG>。如果 字符串包含子字符串 ,则真正的功能就是测试。因此, td [contains(。,'8')]
取字串值 td
(。
)并测试它是否包含任何'8'
子字符串。这可能是你想要的,但通常不是这样。
这个XPath,
// td [。='8']
将全选 td
元素的字符串值 a> 等于 8
。
或者,这个XPath,
// td [normalize-space()='8']
将选择所有 td
元素,其 normalize-space() string-value 等于> 8
。 ( normalize-space() XPath函数剥离前导和尾随空格并用一个空格替换空格字符序列。)
注释:
- 即使8位于另一个元素(例如
a
,b
)内,两者都可以工作,span
,div
等。 - 两者都会 匹配
< td> gr8t< / td>
,< td> 123456789< / td>
等。 - 使用
normalize-space()
将忽略前导或尾随空白
周围8
。
Say we have an HTML table which basically looks like this:
2|1|28|9|
3|8|5|10|
18|9|8|0|
I want to select the cells which contain only 8 and nothing else, that is, only 2nd cell of row2 and 3rd cell of row3.
This is what I tried: //table//td[contains(.,'8')]
. It gives me all cells which contain 8. So, I get unwanted values 28 and 18 as well.
How do I fix this?
EDIT: Here is a sample table if you want to try your xpath. Use the calendar on the left side-https://sfbay.craigslist.org/sfc/
Be careful of the contains()
function.
It is a common mistake to use it to test if an element contains a value. What it really does is test if a string contains a substring. So, td[contains(.,'8')]
takes the string value of td
(.
) and tests if it contains any '8'
substrings. This might be what you want, but often it is not.
This XPath,
//td[.='8']
will select all td
elements whose string-value equals 8
.
Alternatively, this XPath,
//td[normalize-space()='8']
will select all td
elements whose normalize-space() string-value equals 8
. (The normalize-space() XPath function strips leading and trailing whitespace and replaces sequences of whitespace characters with a single space.)
Notes:
- Both will work even if the 8 is inside of another element such as a
a
,b
,span
,div
, etc. - Both will not match
<td>gr8t</td>
,<td>123456789</td>
, etc. - Using
normalize-space()
will ignore leading or trailing whitespace surrounding the8
.
这篇关于如何使用XPath包含()用于特定文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!