<<Previous Post << Complete Tutorial>> Next Post>
In continuation of the previous articles i.e. Relative XPath Expressions – Part 1, Part 2 , Part 3 , Part 4 , Part 5 and Part 6 I am going to explain the next set of Relative XPath Expressions (i.e. Part 7) practically in this article.
Let’s get started.
Selenium Locators – Relative XPath Expression – Part 7
Follow the below steps to practice Relative XPath Expressions on a Web Page:
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 ‘Rel XPath’ option from the ‘Selectors’ dropdown as shown below:
4) Let’s locate the first child of ‘html’ tag using Relative XPath Expression.
First, locate the html tag using the Relative XPath Expression //html
To locate the first child of the above-located html tag, we have to use the Relative XPath Expression //html/*[1]
Note: * in the above expression, means any tag and [1] will locate the first child under html tag
Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating the first child tag under the html tag of the page as shown below:
5) Let’s locate the second child of ‘html’ tag using Relative XPath Expression.
First, locate the html tag using the Relative XPath Expression //html
To locate the second child of the above-located html tag, we have to use the Relative XPath Expression //html/*[2]
Note: * in the above expression, means any tag and [2] will locate the second child under html tag.
Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating the second child tag under the html tag of the page as shown below:
6) Let’s locate the first child of ‘body’ tag using Relative XPath Expression.
First, locate the body tag using the Relative XPath Expression //body
To locate the first child of the above-located body tag, we have to use the Relative XPath Expression //body/*[1]
Note: * in the above expression, means any tag and [1] will locate the first child under body tag
Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating the first child tag under the body tag of the page as shown below:
7) Let’s locate the second child of ‘body’ tag using Relative XPath Expression.
First, locate the body tag using the Relative XPath Expression //body
To locate the second child of the above-located body tag, we have to use the Relative XPath Expression //body/*[2]
Note: * in the above expression, means any tag and [2] will locate the second child under body tag
Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating the second child tag under the body tag of the page as shown below:
8) // in Relative XPath Expression is not only used at the beginning of the XPath Expression, but also can be used in between the Relative XPath Expression as shown below:
Syntax: //tag/tag[@attribute=’value’]/tag//tag
Example: //html//p[@id=’para1′]
Let’s execute the above Relative XPath Expression in ChroPath and observe that it is working and locating the p tag on the page as shown below:
In the above Relative XPath Expression, // in between of the expression will directly go from the html tag to the p tag.
Here ends the seventh part of Relative XPath Expressions.
In the next article, I will demonstrate the next set of Relative XPath Expressions in a practical way.
Next Steps:
- > 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.
<<Previous Post << Complete Tutorial>> Next Post>