</>

Technology

Selenium

Difficulty

Beginner

Interview Question

Which XPath will you prefer to use? Why?

Answer

In automation testing, Relative XPath is always preferred over Absolute XPath.

Reasons:

1. Stability Relative XPath survives UI changes. Even if elements are added/removed above the target element, the XPath still works.

2. Shorter and Readable //input[@id=''username''] vs html/body/div[1]/div[2]/form/div[1]/input

3. Flexible Supports contains(), starts-with(), text() for dynamic elements.

4. Maintainable Less code to update when the UI changes.

Comparison:

XPATH
-- Preferred (Relative)
//input[@id=''username'']

-- Avoid (Absolute)
html/body/div[1]/div[2]/form/div[1]/input

Exception: Absolute XPath may be used temporarily for debugging but should never be committed to your automation framework.

Follow AutomateQA

Related Topics