<<Previous Post << Complete Tutorial>> Next Post>
In the previous articles, I have listed the different types of XPath Axes available and following XPath Axes.
In this article, I am going to explain the next XPath Axes i.e. preceding.
preceding XPath Axes select/traverse all the tag in the HTML document that comes before the current tag, except current parent & ancestor tags (i.e. Current Parent and Grand Parent tags)
Let’s get started with the practical demonstration of preceding XPath Axes.
Selenium Locators – preceding XPath AXES
Follow the below steps to practice preceding XPath Axes along with me:
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 in Chrome Browser where ‘Rel XPath’ is selected as shown below:
3) Let’s first locate the second paragraph in this page using the Relative XPath Expression //p[@id=’para2′]
Execute the above XPath Expression in ChroPath and observe that it is locating the second paragraph on the page as shown below:
4) Now using the above Relative XPath Expression and preceding XPath Axes, let’s create the below Relative XPath expression to locate the first p tag in this page:
//p[@id=’para2′]/preceding::p
Execute the above XPath Expression in ChroPath and observe that it is locating the first paragraph on the page as shown below:
5) Using the Relative XPath Expression created in step 3 and using the preceding XPath Axes, let’s create the below Relative XPath Expression to locate the ‘head’ tag in the page.
//p[@id=’para2′]/preceding::head
Execute the above XPath Expression in ChroPath and observe that it is locating the head tag on the page as shown below:
6) Using the Relative XPath Expression created in step 3 and using the preceding XPath Axes, let’s create the below Relative XPath Expression to locate the ‘title’ tag in the page.
//p[@id=’para2′]/preceding::title
Execute the above XPath Expression in ChroPath and observe that it is locating the title tag on the page as shown below:
7) As already mentioned at the beginning of this article that, using preceding XPath Axes we cannot locate the current parent tag though it comes before the current tag.
Let’s create the below Relative XPath Expression to locate the ‘body’ tag which is the current parent of the ‘p’ tag and comes before the ‘p’ tag.
//p[@id=’para2′]/preceding::body
Execute the above XPath Expression in ChroPath and observe that it will not locate the body tag on the page as shown below:
8) As already mentioned at the beginning of this article that, using preceding XPath Axes we cannot locate the ancestor tags through they come before the current tag.
Let’s create the below Relative XPath Expression to locate the ‘html’ tag which is the ancestor of the ‘p’ tag and comes before the ‘p’ tag.
//p[@id=’para2′]/preceding::html
Execute the above XPath Expression in ChroPath and observe that it will not locate the html tag on the page as shown below:
Here concludes this article.
In the next article, I will start explaining the next XPath AXES – following-sibling
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>