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 //div

Attribute

//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::div

Following-sibling

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

Ancestor

//td[text()='QA']/ancestor::tr

Go beyond the reference

Book a free demo class and apply these commands on live project work.