[英]Use of contains in Selenium XPath
What is the difference between //div[contains(text(),'abc')]
and //div[contains(.,'abc')]
? 之间有什么区别//div[contains(text(),'abc')]
和//div[contains(.,'abc')]
Is "." 是“。” here is used as a regular expression meaning any text that starts with abc
? 这里用作正则表达式意味着任何以abc
开头的文本?
The text()
test selects all text node children of the context node . text()
测试选择上下文节点的所有文本节点子节点 。
The dot ( .
) selects the context node itself. 点( .
)选择上下文节点本身。
As arguments of contains()
function, both .
作为contains()
函数的参数,两者都有.
and text()
are interpreted as string value s which is concatenation of all the child text nodes (see "Element Nodes" ): 和text()
被解释为字符串值 s,它是所有子文本节点的串联(请参阅“元素节点” ):
The string-value of an element node is the concatenation of the string-values of all text node descendants of the element node in document order. 元素节点的字符串值是文档顺序中元素节点的所有文本节点后代的字符串值的串联。
In the case of text()
, the result is computed as concatenation of all direct text node values. 在text()
的情况下,结果计算为所有直接文本节点值的串联。 In the case of .
在这种情况下.
, the text values of all descendant text nodes are concatenated. ,所有后代文本节点的文本值连接在一起。
Consider this: 考虑一下:
<html>
<body>
<div>abc</div>
<div>444 <span>abc</span></div>
<div>def</div>
<div>123 abc</div>
</body>
</html>
//div/text()
selects the following: //div/text()
选择以下内容:
//div/.
: :
Thus, //div[contains(text(),'abc')]
selects only two div
s: 因此, //div[contains(text(),'abc')]
只选择两个div
:
<div>abc</div>
<div>123 abc</div>
while //div[contains(.,'abc')]
selects all div
s with a text node containing abc
: 而//div[contains(.,'abc')]
div
[ //div[contains(.,'abc')]
选择带有包含abc
的文本节点的所有div
:
<div>abc</div>
<div>444 <span>abc</span></div>
<div>123 abc</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.