</>

Technology

Selenium

Difficulty

Beginner

Interview Question

What is the difference between Absolute XPath and Relative XPath?

Absolute XPath traverses from root HTML node; Relative XPath jumps directly to the element using attributes.

Answer

FeatureAbsolute XPathRelative XPath
Starts fromRoot node (html)Anywhere in DOM (//)
Syntax/html/body/div/input//input[@id=''user'']
StabilityFragile — breaks on DOM changesRobust — survives UI changes
LengthLongShort and concise
MaintenanceDifficultEasy
PerformanceSlowerFaster
RecommendedNoYes

Absolute XPath:

XPATH
html/body/div[1]/div[2]/form/input[1]

Equivalent Relative XPath:

XPATH
//form//input[@type=''text'']

Golden Rule: Always use Relative XPath in your automation framework.

Follow AutomateQA

Related Topics