请注意:这个问题是一个更精致的版本上一个问题 https://stackoverflow.com/q/18655765/1839209.
我正在寻找一个 XPath,它可以让我在 HTML 文档中查找具有给定纯文本的元素。例如,假设我有以下 HTML:
<html>
<head>...</head>
<body>
<someElement>This can be found</someElement>
<nested>
<someOtherElement>This can <em>not</em> be found most nested</someOtherElement>
</nested>
<yetAnotherElement>This can <em>not</em> be found</yetAnotherElement>
</body>
</html>
我需要通过文本搜索并且能够找到<someElement>
使用以下 XPath:
//*[contains(text(), 'This can be found')]
我正在寻找一个类似的 XPath,它可以让我找到<someOtherElement>
and <yetAnotherElement>
使用plain text "This can not be found"
。以下不起作用:
//*[contains(text(), 'This can not be found')]
我明白这是因为嵌套em
“破坏”“找不到此”文本流的元素。是否可以通过 XPath 在某种程度上忽略上述嵌套或类似嵌套?
您可以使用
//*[contains(., 'This can not be found')]
[not(.//*[contains(., 'This can not be found')])]
该 XPath 由两部分组成:
-
//*[contains(., 'This can not be found')]
: 运营商.
将上下文节点转换为其字符串表示形式。因此,这部分选择在其字符串表示形式中包含“This can not be find”的所有节点。在上面的例子中,这是<someOtherElement>
, <yetAnotherElement>
and: <body>
and <html>
.
-
[not(.//*[contains(., 'This can not be found')])]
:这将删除其子元素仍包含纯文本“找不到此”的节点。它删除不需要的节点<body>
and <html>
在上面的例子中。
您可以尝试这些 XPathhere http://videlibri.sourceforge.net/cgi-bin/xidelcgi.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)