I am learning selenium Xpath. what are the different ways of Xpath ? what is the difference between //span and//*?
The difference is that
//* selects any element and
//span select elements that are within a
// means that you look for the elements wherever they are ander the current node of xml tree. In your case you start from
// which means that the "current node" is the root node.
Then you specify which element you would like to search.
span is just a concrete node name while
* is a mask accepting any node name. So in your case
//span means "take all nodes with name
span wherever they are in document hierarchy.
//* means take all nodes of the document.
- Open any website and click f12, and click the elements tab:
- Now click ctrl+F, you can see a text field where you can search the element using XPath,text or CSS
- Now click on some element and then give then type the locators //span, and //* in the text field, see what's happening
- '//' means that look for the element from 'Current node'
- //span looks for elements with tag 'span', so on searching you will find only elements with tag name span'
- //* looks for elements with any tag, so it will literally find all the tags in the webpage
Searching "//span" might find the non-span tag in the chrome inspect element, this is because the search field also supports normal text search in the DOM. But in selenium, it will find only tags with name 'Span'