Back to Cheat SheetsLocators · Intermediate
XPath cheat sheet
Axes, functions and the patterns that survive DOM changes.
Basics
Absolute vs relative
Always prefer relative
/html/body/div vs //divAttribute
//input[@id='email']Multiple attributes
//button[@type='submit' and @name='save']Index
(//tr)[3]Functions
contains
//div[contains(@class,'alert')]starts-with
//input[starts-with(@id,'user_')]text()
//a[text()='Logout']normalize-space
Handles stray whitespace
//td[normalize-space()='Paid']Axes
Parent / child
//span[@id='x']/parent::divFollowing-sibling
//label[text()='Email']/following-sibling::inputAncestor
//td[text()='QA']/ancestor::trGo beyond the reference
Book a free demo class and apply these commands on live project work.