HomeSelenium

Selenium Locators – Relative XPath Expressions – Part 6

Selenium Locators – Relative XPath Expressions – Part 6

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

In continuation of the previous articles i.e. Relative XPath Expressions – Part 1Part 2 Part 3Part 4 and Part 5 I am going to explain the next set of Relative XPath Expressions (i.e. Part 6) practically in this article.

Let’s get started.

Selenium Locators – Relative XPath Expression – Part 6

Follow the below steps to practice Relative XPath Expressions on a complex Web Page:

1) 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

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

Relative XPath - ChroPath

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

4) Let’s locate all the hyperlinks in the page using Relative XPath Expression.

As we already know that all the hyperlinks are nothing but anchor tags <a> .. </a> in the HTML code.

In order to locate all the anchor tags in the page, we have to use the Relative XPath Expression //a

Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating all the hyperlinks on the page as shown below:

Relative XPath Expressions - All hyperlinks

5) Let’s find all the hyperlinks on the page that have the href attribute value as http://www.Selenium143.blogspot.com using the Relative XPath Expression.

In order to locate all the anchor tags in the page, we have to use the Relative XPath Expression //a

And, in order to locate all the anchor tags in the page having the href attribute value as http://www.Selenium143.blogspot.com, we have to use the Relative XPath Expression //a[@href=’http://www.Selenium143.blogspot.com’] 

Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating all the hyperlinks having the href attribute value as http://www.Selenium143.blogspot.com on the page as shown below:

Relative XPath Expressions - hyperlinks href

6) Let’s find the first hyperlink on the page that has the href attribute value as http://www.Selenium143.blogspot.com using the Relative XPath Expression.

In order to locate all the anchor tags in the page, we have to use the Relative XPath Expression //a

And, in order to locate the first anchor tag in the page having the href attribute value as http://www.Selenium143.blogspot.com, we have to use the Relative XPath Expression (//a[@href=’http://www.Selenium143.blogspot.com’])[1]

Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating the first hyperlink having the href attribute value as http://www.Selenium143.blogspot.com on the page as shown below:

Relative XPath Expressions - first hyperlink located

7) Let’s find the second hyperlink on the page that has the href attribute value as http://www.Selenium143.blogspot.com using the Relative XPath Expression.

In order to locate all the anchor tags in the page, we have to use the Relative XPath Expression //a

And, in order to locate the second anchor tag in the page having the href attribute value as http://www.Selenium143.blogspot.com, we have to use the Relative XPath Expression (//a[@href=’http://www.Selenium143.blogspot.com’])[2]

Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating the second hyperlink having the href attribute value as http://www.Selenium143.blogspot.com on the page as shown below:

Relative XPath Expressions - second hyperlink located

8) Let’s find out the difference between the below two Relative XPath Expressions:

//a[@href=’http://www.Selenium143.blogspot.com’][1]

and

(//a[@href=’http://www.Selenium143.blogspot.com’])[1]

To explain in a straight forward way, the first one locates at the Parent tag level

And the second one locates at the Page level.

Let me make it more clear by explaining this in a practical way:

On Executing the first Expression in the ChroPath as shown below, it’s not locating the first hyperlink having the href attribute value as ‘http://www.Selenium143.blogspot.com’. Instead, it is locating all the hyperlinks in the web page having the href attribute value as ‘http://www.Selenium143.blogspot.com’.

Relative XPath Expressions - Difference One A

In the above image, all the hyperlink having the given href attribute value are getting located, instead of locating the first hyperlink having the given href attribute value.

In order to find out the reason, we have to inspect each and every hyperlink that got located as shown below:

Relative XPath Expressions - Case One Reason

As explained in the above screenshot, all the located hyperlink tags are the first child’s under their parent tags and hence they got located.

And if you try to locate using //a[@href=’http://www.Selenium143.blogspot.com’][2], in this case, all the child hyperlinks tags having the given href attribute value which are at the second position under their parent tags will be located.

Hence the first case, location strategy is at the tag level.

But in the second case, i.e. (//a[@href=’http://www.Selenium143.blogspot.com’])[1]

Only the first hyperlink having the given href attribute value will get selected, as the location strategy will be at the page level.

Hope you have understood this.

Here ends the sixth 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 *