What is the difference between .// and //* in XPath?

86,453

Solution 1

These expressions all select different nodesets:

.//*[@id='Passwd']

The '.' at the beginning means, that the current processing starts at the current node. The '*' selects all element nodes descending from this current node with the @id-attribute-value equal to 'Passwd'.

What if we don't use dot at the start what it signifies?

Then you'd select all element nodes with an @id-attribute-value equal to 'Passwd' in the whole document.

Just add //* in the XPath -- it highlights --- various page elements

This would select all element nodes in the whole document.

Below mentioned : XPatht's for Gmail Password field are true what is significance of * ?

.//*[@id='Passwd']

This would select all element nodes descending from the current node which @id-attribute-value is equal to 'Passwd'.

//child::input[@type='password']

This would select all child-element nodes named input which @type-attribute-values are equal to 'password'. The child:: axis prefix may be omitted, because it is the default behaviour.

The syntax of choosing the appropriate expression is explained here at w3school.com.

And the Axes(current point in processing) are explained here at another w3school.com page.

Solution 2

There are several distinct, key XPath concepts in play here...

Absolute vs relative XPaths (/ vs .)

  • / introduces an absolute location path, starting at the root of the document.
  • . introduces a relative location path, starting at the context node.

Named element vs any element (ename vs *)

  • /ename selects an ename root element
    • ./ename selects all ename child elements of the context node.
  • /* selects the root element, regardless of name.
    • ./* or * selects all child elements of the context node, regardless of name.

descendant-or-self axis (//*)

  • //ename selects all ename elements in a document.
    • .//ename selects all ename elements at or beneath the context node.
  • //* selects all elements in a document, regardless of name.
    • .//* selects all elements, regardless of name, at or beneath the context node.

With these concepts in mind, here are answers to your specific questions...

  • .//*[@id='Passwd'] means to select all elements at or beneath the context node that have an id attribute value equal to 'Passwd'.
  • //child::input[@type='password'] can be simplified to //input[@type='password'] and means to select all input elements in the document that have an type attribute value equal to 'password'.

Solution 3

The dot in XPath is called a "context item expression". If you put a dot at the beginning of the expression, it would make it context-specific. In other words, it would search the element with id="Passwd" in the context of the node on which you are calling the "find element by XPath" method.

The * in the .//*[@id='Passwd'] helps to match any element with id='Passwd'.

Solution 4

  1. For the first question: It's all about the context. You can see Syntax to know what '.', '..' etc means. Also, I bet you won't find any explanation better than This Link.
  2. Simplified answer for second question: You would generally find nodes using the html tags like td, a, li, div etc. But '*' means, find any tag that match your given property. It's mostly used when you are sure about a given property but not about that tag in which the element might come with, like suppose I want a list of all elements with ID 'xyz' be it in any tag.

Hope it helps :)

Share:
86,453
Mohit
Author by

Mohit

Updated on July 25, 2022

Comments

  • Mohit
    Mohit almost 2 years

    While finding the relative XPath via Firebug : it creates like

    1. .//*[@id='Passwd']--------- what if we dont use dot at the start what it signifies?

    2. Just add //* in the Xpath -- it highlights --- various page elements ---------- what does it signify?

    Below are XPaths for Gmail password fields. What is significance of * ?

    • .//*[@id='Passwd']

    • //child::input[@type='password']

  • Mathias Müller
    Mathias Müller over 8 years
    If in doubt, please have a look at @kjhughes's good answer below.
  • Mathias Müller
    Mathias Müller over 8 years
    This answer should be the accepted one because it is the most accurate and complete, in my opinion.
  • Mathias Müller
    Mathias Müller over 8 years
    Much better now, thanks for editing. Please note that information from w3schools.com is often inaccurate, for instance the confusion between "nodes" and "element nodes" could have stemmed from this incorrect web page.
  • Medi
    Medi almost 6 years
    First thank you for the answer i took it as a reference and stopped using docs because it is really CONCISE and STRICTLY to the point .could you please indicate the difference between context node and current node?
  • kjhughes
    kjhughes almost 6 years