HomeSelenium

Selenium Locators – Relative XPath Expressions – Part 4

Selenium Locators – Relative XPath Expressions – Part 4

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

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

Let’s get started.

Selenium Locators – Relative XPath Expression – Part 4

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 the below-shown button by creating Relative XPath Expression for it.

Relative XPath Expressions - Locate Button

Right click on the above-shown button and select ‘Inspect’ option first:

Relative XPath Expressions - Inspect Button

HTML code for the Inspected Button ‘Button2’ got highlighted as shown below:

Relative XPath Expressions - HTML Button

From the above-highlighted HTML code, we can observe that there is button tag with an id attribute value available and we can create a Relative XPath Expression as shown below:

//button[@id=’but2′]

We can execute the above Relative XPath Expression in ChroPath and observe that it will locate the above-inspected Button i.e. ‘Button2’ as shown below:

Relative XPath Expressions - Located Button2

5) Now let’s find all the tags on this HTML web page, which are having the id attribute value as ‘but2’.

Here in this quest, we are trying to locate any types of tag, may it be input tag, may it be a tag, may it be button tag, etc.

If we have to locate all the input tags, we would have created the Relative XPath Expression as //input

If we have to locate all the anchor tags i.e. anchor a tags, we would have created the Relative XPath Expression as //a

If we have to locate all the button tags, we would have created the Relative XPath Expression as //button

But, what if we have to locate all the tags (input, a, button and other tags) in the HTML. In these cases, we can create a Relative XPath Expression as //*

In the above Relative XPath Expression, * stands for any tag. Hence the Relative XPath Expression //* will locate all the tags in the HTML web page.

But our actual requirement was to locate all the tags that have the id attribute value as but2 in the HTML web page. We can create a Relative XPath Expression for this requirement as //*[@id=’but2′]

Let’s execute the above Relative XPath Expression in the ChroPath and observe that it is locating the ‘Button2’ as shown below:

Note: As there is only one tag in the given HTML page having the id attribute value as ‘but2’, only ‘Button2’ got located.

Relative XPath Expressions - HTML Button Located

6) Locate Button tags having any attribute value as ‘but2’.

First, to locate all the button tags, we have to create the Relative XPath Expression as //button

As it can be any attribute name having value as ‘but2’, we can use * in place of attribute name by creating the Relative XPath Expression as //button[@*=’but2′]

In the above Relative XPath Expression, * means any attribute name.

Let’s execute the above Relative XPath Expression in ChroPath and observe that it is locating the ‘Button2’ button as shown below:

Note: Though the Relative XPath Expression has searched for all the button tags having any attribute name, there is only one attribute having the value as ‘but2’ in the entire page. Hence only ‘Button2’ got located.

Relative XPath Expressions - Any attribute

7) Locate Button tags having id attribute value having any value.

First, to locate all the button tags, we have to create the Relative XPath Expression as //button

Second, to locate all the button tags having the id attribute value having any value, we can create the Relative XPath Expression as //button[@id] 

i.e. If you don’t assign any value to attribute name in the above Relative XPath Expression, that means any value.

Let’s execute the above Relative XPath Expression in ChroPath and observe that it located 5 Button tags having the id attribute value (Here the id attribute value can be anything) as shown below:

Relative XPath Expressions - 5 Buttons

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