XPath - 找不到带有子文本的元素

XPath - not able to find an element with children text

<a>
    This is <var>Me</var> and That is <var> You</var>
</a>

我可以通过以下代码找到包含 "This is" 的元素 "a":

//a[contains(text(),'This is')]

但我无法找到包含 "This is Me and That is You".

的元素 "a"
//a[contains(text(),'This is Me and That is You')]

有没有办法找到包含子文本的元素?

我不确定这是否是您需要的,但您可以使用 string() 来获得所需的结果,

//a[string()='This is Me and That is You']

但需要注意的是,您需要了解所使用字符串的准确信息。

查看工作示例 here

这也可以使用 xpathnormalize-space() 函数找到,该函数从字符串中去除前导和尾随白色-space,将白色 space 字符序列替换为单个 space 和 returns 结果字符串如下:-

//a[normalize-space()='This is Me and That is You']