โ† Back to libraryQuestion 140 of 273
๐ŸงชSeleniumIntermediate

Absolute vs Relative XPath

๐Ÿ“– Detailed Explanation:

Absolute XPath starts with a single slash `/` from the root (html element). It traverses the entire tree from top to bottom. Relative XPath starts with double slashes `//` and can search anywhere in the DOM. Relative XPath is preferred because it is more flexible and robust to structural changes. Example: Absolute `/html/body/div[1]/div[2]/form/input` vs Relative `//input[@name='username']`.

๐ŸŒ Real-World Example:

Absolute XPath is like giving turn-by-turn directions from your hometown. Relative XPath is like saying 'Find a green building anywhere in the city'.

๐ŸŽฏ Scenario-Based Interview Question:

Why is `/html/body/div[1]/div[2]/form/input` considered a 'bad' locator?