Difference between Absolute and Relative XPath

Profile picture for user arilio666

XPath is nothing but the XML path of WebElement. Absolute Xpath and Relative Xpath these two do the same job of tracking down the element but have some variations in their tracking.

Below are the difference between absolute xpath and relative xpath

Point of DifferenceAbsolute PathRelative Path
Starts withSingle Forward Slash. Select the element from the root <html> and cover the whole path to the element. It is also known as complete or Full Xpath.Double Forward Slash. Expression can starts in the middle of the HTML DOM structure.
SpeedFaster. It identify the element very fast.Slower compare to absolute. it will take more time in identifying the element as we specify the partial path not (exact path).
Failure ChancesMore. It Changes Frequently, if there are any changes made in the path of the element then XPath gets failed.Failure chance of well written relative path is very less



if any tag will be added before table the path will fail.


Doesn't matter if anything added before table.

Demo: Difference between Relative and Absolute Xpath

For demo purposes, we will be tracking XPath from our respective https://www.programsbuzz.com/Difference between relative XPath and absolute XPath in Selenium

Absoute XPath

We will be taking absolute xpath for ask doubt which will go like this:


Relative XPath and absolute XPath difference

Taken from the root of the element ask doubt is tracked via absolute XPath.

Relative XPath


absolute XPath and Relative XPath difference

Taken from the exact tag element using the relative XPath method this when faced with any attribute change in the future is very much easy to deal with than absolute XPath.

So, to conclude Relative XPath are always preferred since it is not the complete path from the root element. Hence, there are less chance of becoming invalid.