Selenium Locators – XPath Expression – Wild cards
The following are the two wild cards, we can use in the XPath Expression:
I have already, demonstrated the * wild card in the previous articles on Relative XPath Expression.
In this article, I am going to explain the other wildcard i.e. node().
Before practically demonstrating node() in XPath Expressions, we need to understand the differences between * and node() wild cards.
1) * can be used in XPath Expressions to represent any tag or any attribute name.
Whereas node() can only be used to represent any tags.
2) * is the most used wild card in XPath Expressions.
Whereas node() is least used and can be replaced using * anyhow.
Though node() is least used, we need to know about it for knowledge and interviews purpose.
3) node() should not be used at the end of the Expression. i.e. XPath Expressions should not end with node() wild card. If we add node() at the end of XPath Expression, it becomes invalid.
For example: /html/body/node() is an invalid XPath Expression
Example of valid XPath: /node()/node()/p
Hope, you understood about node() wild card usage in XPath Expressions.
Lets practically use the node() in the XPath Expressions using the below steps:
1) Open http://compendiumdev.co.uk/selenium/basic_web_page.html in Chrome Browser, where ChroPath is already installed (Refer ChroPath installation in Chrome Browser here and its usage in Chrome Browser here ) as shown below:
2) Open ‘ChroPath’ functionality as shown below in the Chrome Browser:
3) Select ‘Abs XPath’ option from the ‘Selectors’ dropdown as shown below:
4) Let’s Execute the below Absolute XPath Expression in ChroPath as shown below:
Absolute XPath Expression: /html/body/p
Now, replace the html and body tags in the above Absolute XPath Expressions with node() wild card as shown below:
Also, you can only replace the body tag in the above Absolute XPath Expression with node() wild card as shown below:
5) Select ‘Rel XPath’ option from the ‘Selectors’ dropdown as shown below:
6) Let’s Execute the below Relative XPath Expression in ChroPath as shown below:
Relative XPath Expression: //body/p
Now, replace the body tag in the above Relative XPath Expressions with node() wild card as shown below:
Here ends this article on wild cards.
In the next article, I will demonstrate the next set of Relative XPath Expressions in a practical way.
- > To learn more about Selenium, continue to the next post (Click on Next Post link below)
- > Check complete Selenium Tutorial Contents here (Click here)
Please leave your questions/comments/feedback below.
Happy Learning ?
On a mission to contribute to the Software Testing Community in all possible ways.