{"id":19561,"date":"2026-09-03T13:18:35","date_gmt":"2026-09-03T13:18:35","guid":{"rendered":"https:\/\/testgrid.io\/blog\/?p=19561"},"modified":"2026-09-03T13:19:09","modified_gmt":"2026-09-03T13:19:09","slug":"xpath-cheat-sheet","status":"publish","type":"post","link":"https:\/\/testgrid.io\/blog\/xpath-cheat-sheet\/","title":{"rendered":"XPath Cheat Sheet: Syntax, Functions, and Examples"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When automated tests interact with web elements, they need locators that can reliably identify the intended element without depending on fragile selectors or XPath expressions. Why?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because even small changes to the page\u2019s DOM structure, attributes, or element hierarchy can cause the locator to stop working and fail the test. XPath gives you a flexible way to describe and locate elements within the DOM.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, with multiple ways to construct XPath expressions, finding the right syntax can be tricky. That\u2019s why we have created this comprehensive XPath cheat sheet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It puts together the essential syntax, functions, axes, and practical locator patterns, so your testers can find, adapt, and maintain locators efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reduce locator maintenance with TestGrid\u2019s AI-powered auto-healing. <a href=\"https:\/\/public.testgrid.io\/signup?form=cotester-starter-package\">Request a free trial<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" style=\"font-size:20px\">XPath Cheat Sheet Overview<\/h2>\n\n\n\n<section class=\"wp-block-custom-tldr-summary tldr-block\"><p class=\"tldr-label\">TL;DR<\/p><ul class=\"tldr-list\"><li><span>XPath enables precise element identification by combining properties such as attributes, text, position, and DOM relationships<\/span><\/li><li><span>Using contains(), starts-with(), normalize-space(), predicates, and indexing create more flexible XPath locators<\/span><\/li><li><span>XPath selectors cheat sheet can help you quickly choose the right expression for common element-location scenarios<\/span><\/li><li><span>An XPath and CSS selector cheat sheet makes it easy to understand how the two locator approaches differ and when each can be useful for building test automation<\/span><\/li><li><span>A Selenium locators cheat sheet can help your testers compare XPath with other Selenium locator strategies when choosing how to identify elements for test automation<\/span><\/li><\/ul><\/section>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is XPath?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">XPath (XML Path Language) is basically a query language that\u2019s used to navigate and select nodes in XML documents and DOM trees representing HTML documents based on their structure, attributes, text, and relationships.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expressions can be used as locator strategies to help automation tools identify elements on a webpage for actions such as clicking, entering text, or verifying content during web automation testing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath also supports selectors, functions, predicates, operators, and axes for precise element queries.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are the Types of XPath?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Absolute<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Absolute XPath helps you identify an element by following a complete path from the document\u2019s root node to the target element. It starts with a single \u2018\/\u2019 and specifies each node along the hierarchy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This approach can enable you to precisely recognize an element, but it\u2019s fragile and can be hard to maintain because changes to the DOM structure can invalidate the entire XPath expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. Relative<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Relative XPath allows you to spot an element without starting from the document\u2019s root node. It generally begins with <strong>\/\/<\/strong> and can locate elements based on attributes, text, or their relationships with other nodes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since it doesn\u2019t depend on the complete DOM hierarchy, relative XPath is comparatively more flexible and reliable to structural changes in a webpage when it is anchored to stable attributes or relationships.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Learn More<\/strong>: <a href=\"https:\/\/testgrid.io\/blog\/xpath-in-selenium\/\">XPath in Selenium<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Essential XPath Concepts<\/strong><\/h2>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">1. Syntax and structure<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An XPath location path consists of one or more location steps separated by \u2018<strong>\/<\/strong>\u2019. Each step can specify an axis, node test, and optional predicates that filter the selected nodes. Common shorthand includes \u2018<strong>\/\/<\/strong>\u2019 for descendant-or-self step, \u2018<strong>@<\/strong>\u2019 for attributes, \u2018<strong>.<\/strong>\u2019 for the context node, and \u2018<strong>..<\/strong>\u2019 for its parent.<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">2. Common functions<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath functions help you perform operations on strings, numbers, node sets, or Boolean values and can make element selection more precise.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common functions you can apply for web automation include contains() for partial matching, starts-with() for prefix matching, normalize-space() for handling whitespace, and position() and last() for position-based selection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The text() node test can also be used to select text nodes when creating XPath expressions.<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">3. Key axes and nodes<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath axes define the relationship between a context node and the nodes you want to select, such as its <strong>parent<\/strong>, <strong>child<\/strong>, <strong>ancestor<\/strong>, <strong>descendant<\/strong>, or <strong>following-sibling<\/strong>. Nodes include elements, attributes, text nodes, comments, processing instructions, and the document\/root node.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Axes help you navigate between these nodes and make it possible to locate elements based on their position or relationship within the page structure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>XPath Locators Cheat Sheet: Ways to Find Elements<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">XPath locators are expressions which tell automation tools how to find specific elements on a webpage. This XPath cheat sheet includes the common locator patterns. This can help your testers choose and adapt the right XPath when creating automated tests.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Find element by tag<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Select elements by specifying their HTML tag name. This can be useful when you need to target all elements of a particular type, such as buttons, links, or input fields. Since multiple elements can share the same tag, you need to combine the tag with attributes or predicates for a specific match.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/tagname<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/button<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Find element by ID<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can select an element using the value of its <strong>id <\/strong>attribute. When an ID is unique within the document, this provides a direct and relatively stable way to locate the element.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/*[@id=&#8217;element-id&#8217;]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/*[@id=&#8217;username&#8217;]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Find element by class<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can locate an element by matching its class attribute. An exact equality check works when the complete class attribute value is known. However, elements often have multiple classes, so exact matching may become unreliable if additional classes are added or their order changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For more reliable matching of a specific class token, use contains() together with normalize-space() to avoid accidentally matching a partial class name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/*[contains(concat(&#8216; &#8216;, normalize-space(@class), &#8216; &#8216;), &#8216; class-name &#8216;)]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/*[contains(concat(&#8216; &#8216;, normalize-space(@class), &#8216; &#8216;), &#8216; btn-primary &#8216;)]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Find element by attribute<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Selecting an element by matching the value of one of its attributes is useful when an element has a distinctive attribute such as <strong>name<\/strong>, <strong>type<\/strong>, <strong>placeholder<\/strong>, <strong>aria-label<\/strong>, or <strong>data-*<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/*[@attribute=&#8217;value&#8217;]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/*[@name=&#8217;username&#8217;]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Find element using text<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Text-based XPath can be useful when an element has distinctive text content but doesn\u2019t have a reliable ID or other attribute. Using normalize-space(.) allows you to match the element\u2019s text content while handling extra whitespace and text contained within nested elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/tagname[normalize-space(.)=&#8217;text&#8217;]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/button[normalize-space(.)=&#8217;Login&#8217;]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Find element using partial text<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>contains()<\/strong> is used when text is dynamic or may contain additional words, making an exact text match too restrictive.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/tagname[contains(text(),&#8217;partial-text&#8217;)]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/button[contains(text(),&#8217;Login&#8217;)]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. Find element using partial attributes<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Partial attribute matching is helpful when an attribute value is dynamic, contains changing text, or is too long to match reliably in full. XPath\u2019s <strong>contains()<\/strong> function lets you match a stable portion of the attribute value instead of requiring an exact match.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/*[contains(@attribute,&#8217;partial-value&#8217;)]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/input[contains(@id,&#8217;user&#8217;)]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. Find elements by their relationships<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">XPath allows you to locate elements based on their position or relationship to other elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can be particularly helpful when the target element lacks a unique ID, class, or other reliable attribute. You can navigate between related elements with axes like parent, child, following-sibling, and preceding-sibling.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>a. By parent<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is used when the target parent has no unique attributes but can be reliably identified through a child element.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/child-element\/parent::parent-element<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/input[@id=&#8217;username&#8217;]\/parent::div<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>b. By child<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You apply the <strong>child <\/strong>axis to locate a child node directly inside a known parent. It enables you to target a specific child when the parent provides a reliable starting point, but the child itself doesn\u2019t have a unique locator.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/parent-element\/child::child-element<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example: \/\/div[@id=&#8217;login-form&#8217;]\/child::input<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>c. By sibling<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>following-sibling<\/strong> and <strong>preceding-sibling<\/strong> axes are useful when you need to identify an element through another element positioned beside it at the same hierarchy level. They allow you to locate a sibling without relying on the target element having a unique attribute.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">XPath expression: \/\/element\/following-sibling::sibling-element<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example (preceding sibling): \/\/element\/preceding-sibling::sibling-element<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also Read<\/strong>: <a href=\"https:\/\/testgrid.io\/blog\/css-selector-in-selenium\/\">How to Use CSS Selectors in Selenium WebDriver<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>XPath Functions Cheat Sheet<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">XPath functions can help you refine element selection, manipulate or compare values, and evaluate conditions when basic XPath syntax is not sufficient. Here\u2019s a useful XPath query cheat sheet covering common functions for automated testing.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Function<\/th><th class=\"has-text-align-left\" data-align=\"left\">Use<\/th><th class=\"has-text-align-left\" data-align=\"left\">XPath Expression<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">starts-with()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Match an attribute value that begins with a specified string<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/*[starts-with(@id,&#8217;user_&#8217;)]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">normalize-space()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Remove leading or trailing whitespace and collapse consecutive spaces before matching<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/*[normalize-space(text())=&#8217;Login&#8217;]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">concat()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Combine multiple strings or attribute values into one string<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/*[concat(@id,&#8217;-&#8216;,@name)=&#8217;user-login&#8217;]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">substring()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Return a substring of a string for matching or comparison<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/*[substring(@id,1,4)=&#8217;user&#8217;]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">substring-before()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Return the part of a string before a specified substring<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/*[substring-before(@id,&#8217;_&#8217;)=&#8217;user&#8217;]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">substring-after()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Return the part of a string after a specified substring<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/*[substring-after(@id,&#8217;_&#8217;)=&#8217;login&#8217;]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">not()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Select elements that don\u2019t satisfy a specified condition<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/*[not(@disabled)]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">position()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Select an element based on its position within a matching node set<\/td><td class=\"has-text-align-left\" data-align=\"left\">(\/\/button)[2]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">last()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Select the last element in a matching node set<\/td><td class=\"has-text-align-left\" data-align=\"left\">(\/\/button)[last()]<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">count()<\/td><td class=\"has-text-align-left\" data-align=\"left\">Count the number of nodes in the node-set selected by an XPath expression<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/*[count(.\/\/input)=2]<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>XPath Axes Cheat Sheet<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">XPath axes help you navigate the relationship between an element and other elements in the DOM. They are useful when a direct locator is not available, and you need to locate an element based on its position or relationship to another element.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Axis<\/th><th class=\"has-text-align-left\" data-align=\"left\">Use<\/th><th class=\"has-text-align-left\" data-align=\"left\">XPath Expression<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\">ancestor<\/td><td class=\"has-text-align-left\" data-align=\"left\">Select all ancestors of the context element that match the specified element<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/element\/ancestor::ancestor-element<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">ancestor-or-self<\/td><td class=\"has-text-align-left\" data-align=\"left\">Select the context element and its matching ancestors<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/element\/ancestor-or-self::element<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">descendant<\/td><td class=\"has-text-align-left\" data-align=\"left\">Select all matching descendants at any level below the context element<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/element\/descendant::descendant-element<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">descendant-or-self<\/td><td class=\"has-text-align-left\" data-align=\"left\">Select the context element and all matching descendants<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/element\/descendant-or-self::element<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">self<\/td><td class=\"has-text-align-left\" data-align=\"left\">Select the context element itself if it matches<\/td><td class=\"has-text-align-left\" data-align=\"left\">\/\/element\/self::element<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Learn More<\/strong>: <a href=\"https:\/\/testgrid.io\/blog\/locators-in-appium\/\">Appium Locator Strategies: A Complete Guide to Finding Elements<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Best Practices for Writing Reliable XPath Locators<\/strong><\/h2>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">1. Keep XPath locators compact and readable<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s best to avoid unnecessarily long XPath expressions with excessive DOM traversal.<a href=\"https:\/\/testgrid.io\/blog\/selenium-testing\/\" data-type=\"link\" data-id=\"https:\/\/testgrid.io\/blog\/selenium-testing\/\"> Selenium<\/a> recommends keeping locators concise and readable because complex traversal can be hard to maintain and may require more processing.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Pro tip<\/strong><br>Aim for the shortest XPath which recognizes the intended element. Try not to reproduce the entire DOM hierarchy when a simpler, stable expression can locate it.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">2. Prefer relative XPath<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Relative XPath does not start from the document root and is evaluated relative to the current context rather than depending on the document\u2019s root and full DOM hierarchy. This generally helps you produce shorter and more maintainable locators which are less sensitive to structural changes on your web page.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Pro tip<\/strong><br>Limit the use of absolute paths such as \/html\/body\/div\/&#8230;. Opt for concise relative paths like \/\/input[@name=&#8217;email&#8217;] when it uniquely identifies the intended element.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">3. Use stable and unique attributes<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When an element has a stable attribute such as an ID or app-specific attribute, use it as the basis of your XPath. Stable attributes are less likely to change when the page structure is modified.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Pro tip<\/strong><br>Select attributes which represent the element\u2019s purpose or identity rather than implementation details, because these may change during UI redesigns or code updates. And this can cause locators to become invalid and automated tests to fail.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Also Read:<\/strong> <a href=\"https:\/\/testgrid.io\/blog\/why-test-automation-fails\/\">7 Standard Reasons Why Test Automation Fails [With Solution]<\/a><\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\">4. Revisit fragile locators<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You should regularly review XPath locators that depend on unstable attributes, DOM structure, or positional assumptions. When your app updates, refactoring these locators will allow you to prevent failures and keep your test suite maintainable.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table class=\"has-fixed-layout\"><tbody><tr><td><strong>Pro tip<\/strong><br>You need to treat locator maintenance as an ongoing task. Reassess failing or frequently modified locators after UI changes. This can minimize repeated test failures.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Build Stable Test Automation with TestGrid<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/testgrid.io\/\">TestGrid<\/a> is an AI-native test automation platform which supports existing Selenium and Appium test suites across <a href=\"https:\/\/testgrid.io\/real-device-testing\">real browsers and mobile devices<\/a>, with parallel execution and CI\/CD integrations including Jenkins, Azure DevOps, and GitHub Actions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">TestGrid also provides AutoHeal capabilities for its codeless test automation. When UI changes affect previously identified elements, AutoHeal can detect locator changes and use alternative locators to help keep tests running.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This can reduce the effort required to maintain tests as your application evolves and minimize test failures caused by changes to UI elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Build, run, and scale reliable automated tests with TestGrid. <a href=\"https:\/\/public.testgrid.io\/signup?form=cotester-starter-package\">Request a free trial<\/a> today.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Frequently Asked Questions<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1788434456417\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is an XPath cheat sheet for Selenium?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>A Selenium XPath cheat sheet is a quick reference for XPath syntax and standard expressions for locating web elements. It usually covers paths, attributes, functions, axes, and practical locator patterns that can be used with Selenium\u2019s By.XPATH() strategy.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788434469484\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is an XML XPath cheat sheet used for?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>An XML XPath cheatsheet gives you the reference syntax for navigating an XML document and selecting specific nodes based on their hierarchy, attributes, relationships, or conditions. It\u2019s helpful when you\u2019re working with XML data, XSLT transformations, or apps that evaluate XPath expressions against XML documents.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788434476165\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">Is there a difference between a CSS XPath cheat sheet and an XPath cheat sheet?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes. CSS selectors and XPath are separate locator strategies. XPath can navigate the document using axes and select nodes based on their attributes, text, and relationships. CSS selectors use selectors and combinators to select elements based on attributes, hierarchy, and other supported conditions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788434488700\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is an XPath injection cheat sheet?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>An XPath injection cheat sheet covers the techniques for understanding, testing, and preventing vulnerabilities that can occur when untrusted input is incorporated into dynamically constructed XPath queries. Attackers can manipulate dynamically constructed XPath expressions to alter query results and, depending on the application&#8217;s implementation, potentially bypass authorization logic or access unauthorized XML data.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788434500941\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What is the difference between XPath selector cheat sheet and XPath locator cheat sheet?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>An XPath selector cheat sheet focuses on XPath expressions and syntax for selecting nodes. An XPath locator cheat sheet emphasizes using those expressions to identify web elements in test automation tools such as Selenium.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>When automated tests interact with web elements, they need locators that can reliably identify the intended element without depending on fragile selectors or XPath expressions. Why? Because even small changes to the page\u2019s DOM structure, attributes, or element hierarchy can cause the locator to stop working and fail the test. XPath gives you a flexible [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":19565,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":"","_members_access_role":[],"_members_access_error":""},"categories":[209],"tags":[],"class_list":["post-19561","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-test-automation"],"acf":[],"images":{"medium":"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2026\/09\/xpath-cheat-sheet-300x169.webp","large":"https:\/\/testgrid.io\/blog\/wp-content\/uploads\/2026\/09\/xpath-cheat-sheet-1024x576.webp"},"_links":{"self":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/19561","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/comments?post=19561"}],"version-history":[{"count":3,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/19561\/revisions"}],"predecessor-version":[{"id":19564,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/posts\/19561\/revisions\/19564"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/media\/19565"}],"wp:attachment":[{"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/media?parent=19561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/categories?post=19561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/testgrid.io\/blog\/wp-json\/wp\/v2\/tags?post=19561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}