Google+

347. XPath Optimization Strategy Four - Use Relative XPath Statements instead of Absolute XPath Statements








Absolute XPath expression has the disadvantage of typing the expression to a particular input structure. Sometimes you need an XPath expression that is flexible in the face of varying input. Hence relative XPath expressions are better and recommended to use.

Now lets follow the below steps to change the Absolute XPath to Relative XPath:

1. Open http://book.theautomatedtester.co.uk/chapter1 in Firefox Browser
2. Select the HTML tag in the FireBug options as shown below:


3. Right click on the 'Home' page link and select 'Inspect Element with FireBug' option as shown below:




4. Ensure that the HTML code related to the inspected 'Home Page' link is high lighted in the HTML code as shown below:



5. Right click on the high lighted HTML code and select 'Copy XPath' option as shown below:



6. Select the Firepath tab from the FireBug options and paste the copied XPath value into the XPath field as shown below and click on 'Eval' button:



7. Ensure that the 'Home Page' element link got high lighted on the page as shown below:


8. But as the XPath is having the Absolute XPath statement as shown below, lets change the Absolute XPath statement in the XPath field to Relative XPath as the Relative XPath statement is recommended over the Absolute XPath.


9. Now lets replace the Absolute XPath Statement in the XPath field with the Relative XPath Statement //a as shown below and click on 'Eval' button:


10. Ensure that the 'Home Page' element link on the page is high lighted as shown below:


This is how we have to change the Absolute XPath to Relative XPath and use in the Selenium Automation code.





Please comment below to feedback or ask questions.

Practice XPath  will be explained in the next post.






No comments: