如何将 XPath contains() 用于特定文本? [英] How to use XPath contains() for specific text?

查看:25
本文介绍了如何将 XPath contains() 用于特定文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个基本如下所示的 HTML 表格:

Say we have an HTML table which basically looks like this:

2|1|28|9|
3|8|5|10|
18|9|8|0|

我想选择只包含 8 个而没有其他的单元格,即只有第 2 行的第 2 个单元格和第 3 行的第 3 个单元格.

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.

这是我尝试过的://table//td[contains(.,'8')].它给了我所有包含 8 的单元格.所以,我也得到了不需要的值 28 和 18.

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?

如果您想尝试使用 xpath,这里有一个示例表.使用左侧的日历-https://sfbay.craigslist.org/sfc/

Here is a sample table if you want to try your xpath. Use the calendar on the left side-https://sfbay.craigslist.org/sfc/

推荐答案

注意 contains() 函数.

Be careful of the contains() function.

使用它来测试元素是否包含值是一个常见的错误.它真正做的是测试字符串是否包含子字符串.所以,td[contains(.,'8')]td (.) 的字符串值并测试它是否包含任何'8' 子串.这可能是您想要的,但通常不是.

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.

这个 XPath,

//td[.='8']

将选择所有 td 元素的 string-value 等于 8.

will select all td elements whose string-value equals 8.

或者,这个 XPath,

Alternatively, this XPath,

//td[normalize-space()='8']

将选择所有 td 元素 normalize-space() 字符串值等于 8.(normalize-space() XPath 函数去除前导和尾随空格并将空格字符序列替换为单个空格.)

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.)

  • 即使 8 在另一个元素内,例如 aabspandiv
  • 两者都匹配gr8t123456789
  • 使用 normalize-space() 将忽略前导或尾随空格围绕 8.
  • 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 the 8.

这篇关于如何将 XPath contains() 用于特定文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆