1. What is the difference between Absolute XPath and Relative XPath, and why is Absolute XPath avoided in enterprise automation?
Absolute XPath begins with a single forward slash ('/') from the root html node (e.g., `/html/body/div[2]/div[1]/form/input[1]`). Any minor UI refactoring or DOM restructuring instantly breaks the locator. Relative XPath begins with double forward slashes ('//') and searches anywhere within the DOM based on element attributes, tag names, or text content (e.g., `//input[@id='username']` or `//button[contains(text(),'Sign In')]`). In enterprise automation, Relative XPath (and stable CSS selectors) is strictly mandated because it isolates test scripts from layout restructuring.
// ❌ Fragile: Breaks whenever a wrapper div is added
By badXPath = By.xpath("/html/body/div[1]/div[2]/form/div[3]/input");
// ✅ Resilient: Targets semantic attributes independently of layout hierarchy
By resilientXPath = By.xpath("//input[@data-testid='user-email' or @name='email']");