Absolute vs Relative XPath Expressions
In this video, we explain the difference between Absolute XPath and Relative XPath.
Absolute XPath:
Not recommended for real-time projects
Used mainly for learning purposes
Relative XPath:
Recommended for real-time usage
More flexible and reliable
Examples of Absolute XPath:
html : /html
head : /html/head
title : /html/head/title
body : /html/body
p tag : /html/body/p
First paragraph : /html/body/p[1]
Second paragraph : /html/body/p[2]
Paragraph with id equals para1 : /html/body/p[@id='para1']
Paragraph with class equals sub : /html/body/p[@class='sub']
Paragraph with class equals main : /html/body/p[@class='main']
Paragraph with id equals para2 : /html/body/p[@id='para2']
Paragraph with id equals para1 and class equals main : /html/body/p[@id='para1'][@class='main']
Paragraph with id equals para2 and class equals sub : /html/body/p[@id='para2'][@class='sub']
Key takeaway:
Absolute XPath helps understand structure, but Relative XPath is better for real-time automation.