Learn XPath
29 lessons in six tracks, from your first predicate to shadow DOM, Appium and scraping. Every lesson has runnable examples, robustness ratings, and a one-click link into the playground.
New to XPath?
Start with the Introduction, work through Getting Started in order, then pick the tracks that match your stack. Already comfortable? Jump to Advanced Patterns or the Testing Tools track for your framework.
Getting Started
Syntax, paths, attributes, predicates, node types and operators. Everything else builds on these.
Introduction to XPath
Learn what XPath is, why it's essential for test automation, and how it compares to CSS selectors.
Relative vs Absolute Paths
Understand why relative XPath expressions are essential for maintainable test automation.
Attribute Selectors
Master the @ syntax for selecting elements by their attributes in XPath.
Predicates: Filtering with [ ]
Predicates are the square-bracket filters that turn a broad selection into exactly the elements you want. Learn how they evaluate, how they chain, and the classic index trap.
Node Types and Wildcards
Understand the seven node types XPath can see, what * and node() really match, and why //DIV finds nothing in an HTML page.
Operators and Comparisons
Equality, inequality, arithmetic and the union operator, including the surprising way comparisons behave when one side is a node-set.
Core Tools
Axes, the complete XPath 1.0 function set, positions and boolean logic.
XPath Axes Navigation
Master the 13 XPath axes to navigate relationships between elements in any direction.
XPath 1.0 Functions Reference
Every function available in browser XPath, grouped by what it does, with the test-automation use case for each and the XPath 2.0 functions you must not reach for.
Position and Indexing
position(), last(), numeric predicates, the difference between //li[1] and (//li)[1], and why ancestor::div[1] is the nearest ancestor rather than the outermost.
Logic: and, or, not
Combine conditions inside predicates, negate correctly with not(), and express 'has none of' without falling into the node-set != trap.
Text Handling
Matching what the user sees: whitespace, nesting, case and the text() versus . distinction.
Text Matching Functions
Learn to select elements by their text content using text(), contains(), and normalize-space().
text() versus . (string value)
The single most misunderstood part of XPath text matching: why contains(text(), 'x') misses matches, when to use . instead, and how nested elements change the answer.
Whitespace and normalize-space()
Why pretty-printed HTML breaks exact text matches, what normalize-space() does and does not fix, and how non-breaking spaces sneak into your locators.
Case-Insensitive Matching
XPath 1.0 has no lower-case() function. Learn the translate() idiom that gives you case-insensitive text and attribute matching in every browser.
Advanced Patterns
Tables, lists, forms, dynamic attributes, SVG, shadow DOM and the anchoring technique behind robust locators.
Working with Tables
Learn strategies for selecting table cells, rows, and data dynamically based on headers and content.
Dynamic IDs and Generated Classes
React, Angular, Vue and CSS-in-JS generate ids and class names that change on every build. Learn to spot them, and the partial-match and anchor strategies that survive them.
Forms and Labels
Locate inputs the way users find them: by their label. Covers for/id association, wrapping labels, aria-labelledby, placeholders, selects, radios and checkboxes.
Lists and Repeated Components
Cards, rows, tiles and list items all look identical to XPath. Learn to select the one you mean by its content, then reach the button inside it, without counting.
SVG, Namespaces and local-name()
Why //svg and //path return nothing in a browser, how namespaces work in the HTML DOM, and the local-name() idiom for icons, charts and icon buttons.
Shadow DOM and iframes
XPath cannot cross a shadow root or an iframe boundary. Learn to recognise both, what each framework offers to get inside, and how XPath fits once you are there.
Anchoring to Stable Context
The technique behind every robust locator: start from a landmark that will not move, then walk a short, meaningful path to the target.
Debugging
Testing in DevTools, decoding error messages, and the truth about XPath performance.
Testing XPath in DevTools
Verify every locator in the browser before it goes into a test: the Elements search box, $x() in the console, document.evaluate, and the Copy XPath trap.
Common Errors and Fixes
The error messages Selenium, Playwright and the browser throw for bad XPath, what each one means, and the mistake that usually caused it.
Performance and Efficiency
Is XPath slow? Where the time actually goes in a locator lookup, which expressions are genuinely expensive, and the two habits that matter more than XPath versus CSS.
Testing Tools
XPath in Selenium, Playwright, Cypress, Puppeteer, WebdriverIO, Appium and scraping libraries.
XPath in Selenium
By.xpath in Java, Python, C# and JavaScript, quoting helpers, relative searches from an element, Selenium 4 relative locators, and where XPath sits in a page object.
XPath in Playwright
The xpath= prefix, auto-detection of // expressions, chaining XPath with CSS and role locators, strict mode, and when Playwright's built-in locators are the better choice.
XPath in Cypress, Puppeteer and WebdriverIO
Cypress needs a plugin, Puppeteer uses the xpath/ prefix, WebdriverIO detects it automatically. What each runner supports, its quirks, and the migration notes.
XPath in Appium (Mobile)
XPath on Android and iOS: the native class names, the attributes that exist, why it is slow on mobile, and the accessibility-id strategy you should reach for first.
XPath for Web Scraping and Data Extraction
The same language, a different goal: extracting values rather than clicking. lxml, Scrapy, parsel and BeautifulSoup, text and attribute extraction, and the XPath 1.0 plus EXSLT regex extension.