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

When searching for relative XPath through Firebug: it is created as

  • .//*[@id='Passwd'] --------- what if we do not use the dot at the beginning, what does it mean?

  • Just add //* to Xpath - it highlights --- various page elements ---------- what does this mean?

The following are the XPaths fields for Gmail. What is the meaning of * ?

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

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

+6
source share
4 answers

All these expressions select different nodes:

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

"." at the beginning means that the current processing starts from the current node. "*" Selects all node nodes that descend from this current node with @id -attribute-value equal to "Passwd".

What if we do not use a dot at the beginning, what does it mean?

You will then select all element nodes with an @id -attribute value equal to "Passwd" throughout the document.

Just add // * to XPath - this highlights --- various page elements

This will result in the selection of all element nodes in the entire document.

Mentioned below: XPatht field for Gmail Password is true, what is the value *?

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

This will allow you to select all nodes of nodes descending from the current node, the value of @id -attribute-value is equal to 'Passwd'.

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

This will allow you to select all children named input , which @type type-attribute-values ​​are equal to "password". The axis prefix of child:: may be omitted since this is the default behavior.

The syntax for selecting the appropriate expression is explained here at w3school.com .

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

+10
source

There are several different XPath key concepts in the game ...

Absolute and Relative XPaths ( / vs.)

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

A named element vs any element ( ename vs * )

  • /ename selects the root element of ename
    • ./ename selects all ename children of the current node.
  • /* selects the root element regardless of name.
    • ./* or * selects all children of the node context, regardless of name.

axis of the male descendant ( //* )

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

Given these concepts, here are the answers to your specific questions ...

  • .//*[@id='Passwd'] means the selection of all elements at or below the current node context, which has an id attribute value equal to 'Passwd' .
  • //child::input[@type='password'] can be simplified to //input[@type='password'] and means the selection of all input elements in the document with the type attribute equal to 'password' .
+16
source

A point in XPath is called a context element expression. Putting a dot at the beginning of an expression will make it contextual. In other words, it will look for the element with id="Passwd" in the node context on which you call the "find element by XPath" method.

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

+2
source
  • For the first question: all about context. You can see Syntax to find out what ".", ".." etc. mean. In addition, I am sure you will not find any explanation better than this link .
  • A simplified answer to the second question: usually you will find nodes using html tags such as td, a, li, div, etc. But "*" means that you will find any tag that matches your specified property. It is mainly used when you are sure of the specified property, but not about this tag, in which the element may be present, for example, I want the list of all elements with the identifier "xyz" to be in any tag.

Hope this helps :)

+1
source

All Articles