<<Previous Post << Complete Tutorial>> Next Post>>
In the previous articles, I have practically demonstrated all the Absolute XPath Expressions and Relative XPath Expressions in detail.
In this article, I am going to explain the Advantages of using Relative XPath Expressions over Absolute XPath Expressions.
As already explained in the previous articles ‘Advantages and disadvantages of Absolute XPath Expressions‘, we should only use Absolute XPath Expressions for learning and should not use them for locating the UI elements on the web pages in real time projects. The major disadvantage of using Absolute XPath Expressions is ‘Due to small changes in the UI, the Absolute XPath Expression of a UI element may change and won’t work.’.
But coming to the advantage of using Relative XPath Expressions over Absolute XPath Expressions, they are reliable when compared to the Absolute XPath Expressions and can sustain small changes of the UI elements on the web page.
Pre-requisites: In order to understand this article, you should go through the below posts first:
- Installing Chropath Add-on on Chrome Browser
- Using Chropath Add-on in Chrome Browser
- Installing Selenium IDE in Chrome Browser
- Launching Selenium IDE in Chrome Browser
- Advantages and disadvantages of Absolute XPath Expressions
Let’s get started.
Advantages of Relative XPath Expressions over Absolute XPath Expressions
Following is the major advantage of Relative XPath Expressions over Absolute XPath Expressions. Let me explain this with a practical example:
Let’s make a small change in the UI, by moving the ‘Button2’ button from the left side of the page to the right side of the page. After making the above change, let’s see whether the Original Relative XPath Expression captured when the button on the left side of the page is still working after moving the button to the right side of the page.
Follow the below practical steps to check the above:
1) Open http://omayo.blogspot.com/ and scroll down to view the ‘Button2’ button which is on the right side of the application as shown below:
2) Right click on the ‘Button2’ button, select ‘Inspect’ option
3) Select the ‘Elements’ tab in the Chrome Developer option and then find & select ‘ChroPath’ option as shown below:
4) Copy the Relative XPath Expression of the above ‘Button2’ button on the left side of the page by selecting the ‘Copy’ option as shown below:
5) Now open any notepad in your machine and paste the above copied Absolute XPath Expression for the ‘Button2’ button when it is on the left side of the page as shown below:
6) Open Selenium IDE and enter the above Relative XPath Expression and observe that the Relative XPath Expression is able to locate the Button2 button.
7) As a developer of the above application, I am moving the ‘Butt0n2’ button from its original place i.e. right side of the page to the left side of the page as shown below:
Note: You cannot move the button to the right side as you are not the owner of http://omayo.blogspot.com/ application. Only I can do it as shown below:
8) Now copy the original Relative XPath Expression pasted in Step 5 into a NotePad software and enter it into the Selenium IDE and observe that it still locates the Button2 button even after moving it to the right side of the page:
Hence the major advantage of Relative XPath Expressions over Absolute XPath Expressions is that it will be able to locate the UI elements on the web pages even after making small changes in the UI.
Let’s find out the reason for behind it, by following the below steps:
9) After moving the ‘Button2’ button to the right side of the page, let’s check whether there is any change in the Relative XPath Expression. Right click on the ‘Button2’ button which is moved to the right side of the page, select ‘Inspect’ option as shown below:
10) Copy the Relative XPath Expression of the above ‘Button2’ button on the right side of the page by selecting the ‘Copy’ option as shown below:
11) Paste the above copied Relative XPath Expression for the ‘Button2’ button when it is on the right side of the page as shown below:
12) Now compare the Relative XPath Expressions of the ‘Button2’ button when the button is on the left side and right side of the page as shown below:
Observe that even after moving the UI Button ‘Button2’ on the application from the left side of the page to the right side of the page, the Relative XPath Expression is same and has not even changed a bit.
Hence this is the major advantage of Relative XPath Expressions over Absolute XPath Expressions, where the Expression is reliable and not changing on performing small changes on the UI.
In the next article, I will start explaining the next locator type ‘CSS Selectos’ from scratch and in detail.
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>>