Html XPath //div[contains(text(), 'string')] 无法选择包含 'string' 的 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27208398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XPath //div[contains(text(), 'string')] fails to select divs containing 'string'
提问by Elangovan S
This is the HTML code:
这是 HTML 代码:
<div> <span></span> Elangovan </div>
I want to write an XPath for the div
based on its contained text. I tried
我想div
为其包含的文本编写一个 XPath 。我试过
//div[contains(text(),'Elangovan')]
but this is not working.
但这不起作用。
回答by alecxe
回答by kjhughes
Alternatively to alecxe's correct answer (+1), the following slightly simpler and somewhat more idiomatic XPath will work the same way:
作为 alecxe 的正确答案 (+1) 的替代方案,以下稍微简单且更惯用的 XPath 将以相同的方式工作:
//div[contains(., "Elangovan")]
The reason that your original XPath with text()
does not work isthat text()
will select all text node children of div
. However, contains()
expects a string in its first argument, and when given a node set of text nodes, it only uses the first one. Here, the first text node contains whitespace, not the sought after string, so the test fails. With the implicit .
or the explicit string()
first argument, all text node descendants are concatenated together before performing the contains()
test, so the test passes.
究其原因,你的初始的XPath与text()
不工作是text()
将选择的所有文本子节点div
。但是,contains()
在它的第一个参数中需要一个字符串,并且当给定一个文本节点的节点集时,它只使用第一个。在这里,第一个文本节点包含空格,而不是寻求的字符串,因此测试失败。使用隐式.
或显式string()
第一个参数,所有文本节点后代在执行contains()
测试之前连接在一起,因此测试通过。
回答by Wayne
To make @kjhughes's already good answer just a little more precise, what you're really asking for is a way to look for substrings in the div
's string-value:
为了使@kjhughes 已经很好的答案更加精确,您真正需要的是一种在div
's string-value 中查找子字符串的方法:
For every type of node, there is a way of determining a string-value for a node of that type. For some types of node, the string-value is part of the node; for other types of node, the string-value is computed from the string-value of descendant nodes.
对于每种类型的节点,都有一种方法可以确定该类型节点的字符串值。对于某些类型的节点,字符串值是节点的一部分;对于其他类型的节点,字符串值是根据后代节点的字符串值计算的。
Both the context node (.
or the div
itself) and the set of nodes returned by text()
-- or any other argument! -- are first converted to strings when passed to contains
. It's just that they're converted in different ways, because one refers to a single element and the other refers to a node-set.
上下文节点(.
或div
本身)和由text()
-- 或任何其他参数返回的节点集!-- 传递给 时首先转换为字符串contains
。只是它们以不同的方式转换,因为一个是指单个元素,另一个是指节点集。
A single element's string-value is the concatenation of the string-values of all its text node descendants. A node-set's string-value, on the other hand, is the string-value of the node in the set that is first in document order.
单个元素的字符串值是其所有文本节点后代的字符串值的串联。另一方面,节点集的字符串值是集合中按文档顺序排在第一位的节点的字符串值。
So the real difference is in what you're converting to a string and how that conversion takes place.
因此,真正的区别在于您要转换为字符串的内容以及转换的发生方式。