What is the difference between //div[contains(text(),'abc')]
and //div[contains(.,'abc')]
?
Is "." here is used as a regular expression meaning any text that starts with abc
?
What is the difference between //div[contains(text(),'abc')]
and //div[contains(.,'abc')]
?
Is "." here is used as a regular expression meaning any text that starts with abc
?
The text()
test selects all text node children of the context node.
The dot (.
) selects the context node itself.
As arguments of contains()
function, both .
and text()
are interpreted as string values which is concatenation of all the child text nodes (see "Element Nodes"):
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. 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/.
:
Thus, //div[contains(text(),'abc')]
selects only two div
s:
<div>abc</div>
<div>123 abc</div>
while //div[contains(.,'abc')]
selects all div
s with a text node containing abc
:
<div>abc</div>
<div>444 <span>abc</span></div>
<div>123 abc</div>