Below are the difference between absolute and Relative XPath:
Point of Difference | Absolute Path | Relative Path |
---|---|---|
Starts with | Single 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. |
Speed | Faster. 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 Chances | More. 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 |
Example |
/html/head/body/form/table/tbody/tr/th if any tag will be added before table the path will fail. |
//table/tbody/tr/th Doesn't matter if anything added before table. |
Chrome gives you option to select full XPATH and relative XPATH, For example this is the examples of Logo of automationpractice.com website
#full XPATH by chrome
/html/body/div/div[1]/header/div[3]/div/div/div[1]/a/img
#Relative XPATH by Chrome
//*[@id="header_logo"]/a/img
#We can further shorten this path like this
//img[@alt='My Store']
All path will find the same element but the last one is shorter and efficient one and failure chances of it are comparatively less then previous two.
Video Tutorial