Skip to main content

Advanced XPath

Advanced XPath techniques are used when basic locators are insufficient. They are extremely common in dynamic, modern web applications.

Mastering these patterns greatly reduces locator failures.


contains()

Used when attribute values are dynamic.

//input[contains(@id,'user')]

When to Use

  • Dynamic IDs
  • Partial attribute matches

starts-with()

Matches attributes that start with a specific value.

//div[starts-with(@class,'menu-')]

Useful when prefixes are stable.


text()

Used to locate elements based on visible text.

//button[text()='Login']

⚠️ Sensitive to UI text changes.


AND / OR Conditions

AND

//input[@type='text' and @name='email']

OR

//input[@id='email' or @name='email']

Use AND for precision, OR for flexibility.


XPath Axes (Very Important)

Parent

//input[@id='email']/parent::div

Child

//div[@id='form']/child::input

Following-Sibling

//label[text()='Username']/following-sibling::input

Preceding-Sibling

//input[@id='password']/preceding-sibling::label

Axes are powerful for complex DOMs.


Index Usage (Use Carefully)

(//input[@type='text'])[1]

⚠️ Avoid unless no other option exists.


Common Mistakes ❌

  • Overusing contains()
  • Relying on indexes
  • Writing overly complex XPath
  • Ignoring DOM structure

Best Practices ✅

  • Prefer attributes over text
  • Use axes for stability
  • Keep XPath readable
  • Validate uniqueness

Key Takeaways

  • Advanced XPath handles dynamic elements
  • Axes improve locator stability
  • Simpler XPath is better XPath