HomeSelenium

Selenium Locators – preceding-sibling XPath AXES

Selenium Locators – preceding-sibling XPath AXES

<<Previous Post                            << Complete Tutorial>>                              Next Post>

In the previous articles, I have listed the different types of XPath Axes available and explained the followingpreceding  and following-sibling XPath Axes.

In this article, I am going to explain the next XPath Axes i.e. preceding-sibling.

preceding-sibling XPath Axes select/traverse all the sibling tags in the HTML document that comes before the current tag.

siblings in the above statements mean brother and sister level tags (i.e. tags at the same level or children of the same parent)

Hence all the brother and sister tags that come before the current tag will be located using preceding-sibling XPath Axes.

Let’s get started with the practical demonstration of preceding-sibling XPath Axes.

Selenium Locators – preceding-sibling XPath AXES

Follow the below steps to practice preceding-sibling 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:

last() XPath Function - Website Launch

2) Open ChroPath in Chrome Browser where ‘Rel XPath’ is selected as shown below:

last() XPath Function - Rel XPath

3) Let’s first locate the ‘body’ tag in this page using the Relative XPath Expression //body

Execute the above XPath Expression in ChroPath and observe that the ‘body’ tag on the page got located as shown below:’

preceding-sibling XPath AXES - body tag

4) Now using the above Relative XPath Expression and preceding-sibling XPath Axes, let’s create the below Relative XPath expression to locate the ‘head’ sibling tag that comes before the ‘body’ tag.

//body/preceding-sibling::head

Execute the above XPath Expression in ChroPath and observe that it is locating the head tag on the page as shown below:

preceding-sibling XPath AXES - head tag

5) Now let’s locate the sibling ‘p’ tag in this page that comes before the second ‘p’ tag in the page.

To locate the second ‘p’ tag in the page, we have to use the Relative XPath Expression //p[@id=’para2′]

Using the above Relative XPath Expression and preceding-sibling XPath Axes, let’s create the below Relative XPath expression to locate the ‘p’ sibling tag that comes before the second ‘p’ tag.

//p[@id=’para2′]/preceding-sibling::p

Execute the above XPath Expression in ChroPath and observe that the first ‘p’ tag on the page got located as shown below:

preceding-sibling XPath AXES - p tag

6) Open http://omayo.blogspot.com/ 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:

Relative XPath - More complex application

Open ‘ChroPath’ functionality as shown below in the Chrome Browser:

Relative XPath - ChroPath

Select ‘Rel XPath’ option from the ‘Selectors’ dropdown as shown below:

First, let’s locate the 4th div tag that comes after the body tag by executing the Relative XPath Expression //body/div[4]  in the ChroPath as shown below:

preceding-sibling XPath AXES - div 4

Using the above Relative XPath Expression and preceding-sibling XPath Axes, let’s create the below Relative XPath expression to locate all the sibling div tags which come before the fourth div tag:

//body/div[4]/preceding-sibling::div

Execute the above XPath Expression in ChroPath and observe that all the sibling div tags that come before the fourth div tag got located as shown below:

preceding-sibling XPath AXES - preceding divs

Here concludes this article.

In the next article, I will start explaining the next XPath AXES – parent

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 🙂

About Me > Arun Motoori

On a mission to contribute to the Software Testing Community in all possible ways.

<<Previous Post                            << Complete Tutorial>>                              Next Post>

If you like this article, share it on LinkedIn using the below button:

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *