The Ultimate XPath Locators Cheat Sheet for Web Automation and Testing

XPath (XML Path Language) is a fundamental tool in web automation and testing, allowing developers and testers to navigate through XML and HTML documents to locate elements. Whether you’re working with Selenium, Cypress, or other automation frameworks, understanding how to write and use XPath locators effectively is crucial for identifying elements and interacting with them efficiently.

This XPath Locators Cheat Sheet provides a quick reference guide to help you master the essential XPath expressions and syntax. From basic locators to advanced functions and axes, this cheat sheet is designed to streamline your web automation tasks by offering a handy compilation of common and useful XPath techniques. Whether you’re a beginner or an experienced user, this guide will ensure you have the necessary tools at your fingertips to write optimal and efficient XPath expressions.

XPath Basics

XPath PatternDescriptionExampleResult
//tagnameSelects all elements of a specific tag//divAll <div> elements
//tagname[@attribute='value']Selects elements with a specific attribute//input[@type='text']<input> elements with type="text"
//tagname[text()='text']Selects element with exact text//button[text()='Submit']Button with “Submit” text
(//tagname)[1]Selects the first element in a list(//input)[1]First <input> element
(//tagname)[last()]Selects the last element in a list(//div[@class='item'])[last()]Last <div> with class='item'
(//tagname)[n]Selects the nth element in a list(//li)[3]Third <li> element

XPath Functions

XPath PatternDescriptionExampleResult
//tagname[contains(@attribute, 'value')]Partial match for attribute values//a[contains(@href, 'login')]<a> with “login” in href
//tagname[starts-with(@attribute, 'value')]Matches the beginning of an attribute//div[starts-with(@class, 'alert')]<div> with class starting with “alert”
//tagname[contains(text(), 'value')]Partial text match//p[contains(text(), 'Welcome')]<p> with “Welcome” in text
//tagname[normalize-space(text())='value']Removes leading/trailing spaces//h2[normalize-space(text())='Title']<h2> with normalized text “Title”

XPath Logical Operators

XPath PatternDescriptionExampleResult
//tagname[@attribute='value1' or @attribute='value2']OR condition for multiple attributes//input[@type='text' or @type='email']<input> with type="text" or type="email"
//tagname[@attribute1='value1' and @attribute2='value2']AND condition for multiple attributes//button[@class='btn' and @disabled='true']<button class='btn' disabled='true'>

XPath Axes

XPath PatternDescriptionExampleResult
//tagname[@attribute='value']/ancestor::ancestor_tagSelects ancestor elements//span[@class='price']/ancestor::divClosest <div> ancestor of <span>
//tagname[@attribute='value']/descendant::descendant_tagSelects descendant elements//div[@class='container']/descendant::pAll <p> within <div class='container'>
//tagname[@attribute='value']/following-sibling::sibling_tagSelects siblings after current node//h3[@class='title']/following-sibling::p<p> following <h3> with class="title"
//tagname[@attribute='value']/preceding-sibling::sibling_tagSelects siblings before current node//div[@class='info']/preceding-sibling::h2<h2> before <div class='info'>
//tagname[@attribute='value']/parent::parent_tagSelects the parent element//span[@class='info']/parent::divParent <div> of <span class='info'>
//tagname[@attribute='value']/self::tagnameSelects the current node itself//input[@id='search']/self::inputThe <input id='search'> element itself

XPath for Multiple Attribute Conditions

XPath PatternDescriptionExampleResult
//*[@attribute='value']Selects any element with a specific attribute//*[@id='header']Any element with id="header"
//tagname[@attribute='value' and @attribute='value']Multiple attributes selection//input[@type='text' and @name='username']<input type='text' name='username'>

XPath for Dropdown Options

XPath PatternDescriptionExampleResult
//select[@id='dropdown']/option[text()='Option Name']Selects option within dropdown//select[@id='dropdown']/option[text()='Option Name']Option with text “Option Name” in dropdown id='dropdown'

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top