Absolute and Relative Xpath
If we write xpath expression from the root node, then it is called Absolute xpath.
<a href=”url” /> link 1
<a href=”url” /> link 2
<input type=text id=input a1 />
<input type=text id=input a2 />
<input type=text id=input b1 />
<input type=text id=input b2 />
<a href=”url” /> link 3
/html/body/div/input => a1, a2, b1, b2
/html/body/div/input => a2
/html/body/div/input =>a1, b1
/html/body/div/a => link 3
Here we can see that the xpaths are written from the root node and so these are absolute xpaths.
Relative Xpath :
//input =>a1, a2, b1, b2
//div/input => a1,b1
//div/input => b2
//div/a => link1, link 2
If you write the xpath in the above format, it is called Relative Xpath. If begins with double slash “//’ which represents search entire web page.
Note 1 :
If we want to narrow down the search, use appropriate tag name after “//”.
Both absolute and relative xpath expressions are written based on the position. Hence, it is called Xpath By Position.
The Disadvantage in Xpath By Position is that the programmer has to remember entire html tree structure as well as the positions of the tags.
If we write xpath expression by position, the expression will be very lengthy. In order to overcome this, we go for Xpath By Attribute.
General Syntax For Xpath by Attribute :
//tagName[@attribute =/ or // AttributeValue //or /]