I know absolute XPath will return the inspected node from root node in XML tree.
But I am not able to understand the meaning of .//
used in XPath to inspect/find a node.
0 votes. Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.
Difference between “/” and “//” in XPathSingle slash is used to create absolute XPath whereas Double slash is used to create relative XPath. 2. Single slash selects an element from the root node. For example, /html will select the root HTML element.
By adding '//*' in XPath you would be selecting all the element nodes from the entire document. In case of the Gmail Password fields, .//*[@id='Passwd'] would select all the element nodes descending from the current node for which @id-attribute-value is equal to 'Passwd'.
It starts with the forward slash '/' . Generally, Absolute XPath is not recommended because in future any of the web element when added or removed then Absolute XPath changes. Relative XPath; In this, XPath begins with the double forward slash '//' which means it can search the element anywhere in the Webpage.
.
is the current node; it is short for self::node()
.
//
is the descendant-or-self axis; it is short for /descendant-or-self::node()/
.
Together, .//
will select along the descendent-or-self axis starting from the current node. Contrast this with //
which starts at the document root.
Consider the following HTML:
<html>
<body>
<div id="id1">
<p>First paragraph</p>
<div>
<p>Second paragraph</p>
</div>
</div>
<p>Third paragraph</p>
</body>
</html>
//p
will select all paragraphs:
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
On the other hand, if the current node is at the div
element (with @id
of "id1"
), then .//p
will select only the paragraphs under the current node:
<p>First paragraph</p>
<p>Second paragraph</p>
Notice that the third paragraph is not selected by .//p
when the current node is the id1
div
because the third paragraph is not under that div
element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With