What is the difference between ".//", "//","./" and "/" in XPath while finding element in Selenium? Describe.
3 Answers
XPath Definitions:
.// - Find something that is a descendant of the current node
// - Find something that is anywhere in the DOM
./ - Find a child node of the current node
/ - Find a child of the root of the document
However in Selenium .// and // do not follow the XPath specs and mean the same thing, which in this case is find something anywhere in the DOM.
*Edit*
Since this seems to be somewhat controversial, here's some additional information.
Selenium uses wicked good XPath in its JavaScript atoms (See the Selenium codebase).
Wicked good XPath while fast, is not a fully compliant XPath implementation and doesn't implement // correctly in all cases hence the assertion above that you should treat both .// and // as an operator to find anything in the DOM (See this wicked good XPath issue)

- 7,281
- 26
- 49
-
1It's a shame that the "Author of Mastering Selenium WebDriver" states that `.//` and `//` in Selenium mean the same thing – Dmitri T May 24 '19 at 07:38
-
From an XPath point of view they don't, from a Selenium point of view they do because the default fallback is a JavaScript XPath library that is not fully standards compliant. If you use `.//` it may work sometimes, but it will not work all the time. – Ardesco May 24 '19 at 07:41
-
It's a double shame that the "core contributor the the JMeter Maven Plugin" doesn't actually know how Selenium works. https://stackoverflow.com/a/56099974/2897748 – Dmitri T May 24 '19 at 07:44
-
Unfortunately there are an awful lot of half-baked tutorials on XPath in Selenium so it is very hard to discover the truth, but I strongly suspect this statement is inaccurate. Remember that leading `//` and `.//` do mean the same thing if the context item is the root node of the document, which at the top level (that is, outwith a predicate) will generally be true in Selenium. – Michael Kay May 24 '19 at 07:46
-
Selenium uses wicked good XPath as a fallback (https://github.com/SeleniumHQ/selenium/blob/master/javascript/atoms/locators/xpath.js#L44). Wicked good XPath is not a fully compliant XPath implementation https://github.com/google/wicked-good-xpath/issues/43. @DmitriT Please check your facts – Ardesco May 24 '19 at 07:57
-
This could be helpful. https://stackoverflow.com/questions/35606708/what-is-the-difference-between-and-in-xpath – Atul May 24 '19 at 10:23
.//
- relative selection of all nodes matching the expression//
- absolute selection of all nodes matching the expression./
- is not a valid XPath expression/
- selects root node of the document
More information:

- 159,985
- 5
- 83
- 133
-
1I agree your XPath is correct from a standards point of view, however XPath in Selenium is not 100% standards compliant which is what causes a lot of confusion. This question is asking about using XPath with Selenium, not in a standards compliant way. – Ardesco May 24 '19 at 08:14
-
It's the matter of context, if you use `driver::findElementByXpath` - there is no difference between absolute and relative locators, however **there is the difference** when you use `webElement::findElementsByXpath` – Dmitri T May 24 '19 at 08:17
-
You don't seem to understand what I'm saying. Wicked Good XPath is broken! If Selenium uses it to try and parse // it doesn't treat it as a synonym of ::descendant-or-self, instead it treats it as a wildcard search across the DOM. This means that the relative node you supply before the // is irrelevant – Ardesco May 24 '19 at 08:46
Meaning of dot (.) in xpath :
- Single Dot (.) - It points to current element.
- Double Dot (..) - It points to parent element.
Meaning of '/' single slash and '//' double slash :
- Single slash '/' - It search for child element.
- Double slash '//' : It search for child element as well as child's child element. In short it work as descendant.
- '//' it is a macro , it's expansion is : /descendant-or-self::node()/
Answer to question :
- // - Search descendant or self node in DOM
.// - Search descendant or self node with respective to the current node
/ - Search child node
- ./ - Search child node with respective to the current node
For more information refer : https://www.linkedin.com/pulse/basics-xpath-pritam-maske/

- 2,670
- 2
- 22
- 29