</>

Technology

Selenium

Difficulty

Beginner

Interview Question

What is a relative XPath?

Relative XPath starts from anywhere in the DOM using // and is more stable and preferred over absolute XPath.

Answer

A Relative XPath starts from anywhere in the HTML document using the double-slash // notation. It is the preferred approach in automation testing.

Syntax: Starts with //
XPATH
//input[@id=''username'']
//button[text()=''Login'']
//div[@class=''header'']//a[@href=''/home'']

Advantages over Absolute XPath:

  • Survives UI structural changes above the target element
  • Shorter, more readable
  • Supports powerful functions like contains(), starts-with(), text()

Common patterns:

XPATH
//input[@name=''email'']
//button[text()=''Submit'']
//input[contains(@id,''user'')]
//input[starts-with(@class,''btn'')]
//div[@class=''form'']//input[@type=''text'']
//label[@for=''email'']/following-sibling::input

Follow AutomateQA

Related Topics