您可以使用//*[contains(., 'This can not be found')]
.
上下文节点.
在与“找不到此”进行比较之前将被转换为其字符串表示形式。
不过要小心因为你正在使用//*
,所以它会匹配ALL嵌入包含该字符串的元素。
在您的示例中,它将匹配:
<someOtherElement>
- and
<body>
- and
<html>
!
您可以通过定位文档中的特定元素标签或特定部分(a<table>
or <div>
具有已知的 id 或类)
编辑评论中有关如何查找与文本条件匹配的最多嵌套元素的OP问题:
这里接受的答案建议//*[count(ancestor::*) = max(//*/count(ancestor::*))]
选择最嵌套的元素。我认为这只是 XPath 2.0。
当与你的子字符串条件结合时,我能够在这里测试一下与此文件
<html>
<head>...</head>
<body>
<someElement>This can be found</someElement>
<nested>
<someOtherElement>This can <em>not</em> be found most nested</someOtherElement>
</nested>
<someOtherElement>This can <em>not</em> be found</someOtherElement>
</body>
</html>
并使用这个 XPath 2.0 表达式
//*[contains(., 'This can not be found')]
[count(ancestor::*) = max(//*/count(./*[contains(., 'This can not be found')]/ancestor::*))]
并且它匹配包含“This can not be find most Nested”的元素。
可能有一种更优雅的方法来做到这一点。