HomeSelenium

Selenium Locators – Relative XPath Expressions – Part 7

Selenium Locators – Relative XPath Expressions – Part 7

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

In continuation of the previous articles i.e. Relative XPath Expressions – Part 1Part 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:

Absolute XPath - Opening Basic Application

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

Absolute XPath - Open ChroPath

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:

Relative XPath Expressions - html first child

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:

Relative XPath Expressions - html second child

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:

Relative XPath Expressions - body first child

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:

Relative XPath Expressions - body second child

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:

Relative XPath Expressions - double

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 🙂

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 *