Techniques 7 min read

XPath for Web Scraping: Best Cheat Sheets & Quick References

XPath for web scraping made easy: the best cheat sheets and quick references with syntax, axes, functions, and real extraction examples. Grab a reference.

ST
Scraping.Pro Team
Data collection for business needs
Published: 25 June 2026

There's something reassuring about having a good cheat sheet pinned next to your monitor. When the syntax slips your mind mid-task, you want an answer in two seconds, not a tab full of spec prose. XPath is one of those topics I reach for a reference on again and again — the core idea is simple, but the axes, functions, and predicate tricks are easy to forget if you don't use them every day.

This post is a curated tour of the XPath references I actually trust, grouped by what they're good for: the canonical specifications, fast interactive cheat sheets, the docs for the libraries that run XPath under the hood, and the browser tools that let you test expressions live. There's also a short built-in cheat sheet near the end for the expressions you'll use most.

A One-Minute Refresher

XPath (XML Path Language) is a query language for pointing at parts of a tree-shaped document. You write a path expression — a series of steps separated by / — and the engine returns the nodes that match. It's the backbone of XSLT and XQuery, the standard locator strategy in tools like Selenium and Playwright, and a workhorse for web scraping because it can walk an HTML document by structure, attributes, or text content.

A quick taste:

code
//div[@class="card"]//a/@href

Read right to left, that says: find every div with a class of card, anywhere in the document; look at all descendant a elements; and return their href attribute values.

Which Version Are You Actually Using?

This matters more than people expect, because a function that exists in one version may be missing in another.

  • XPath 1.0 — the version most HTML-scraping and browser tools still default to. It's compact and covers the basics, but lacks lower-case(), matches(), tokenize(), and proper data typing.
  • XPath 2.0 — adds regular expressions, richer string handling, sequences, and a real type system.
  • XPath 3.0 — introduces inline (anonymous) functions and function items as first-class values.
  • XPath 3.1 — the current standard, finalized as a W3C Recommendation in March 2017. Its headline additions are maps and arrays plus the arrow operator => for chaining, largely to make working with JSON easier.

A practical heads-up for scraping: Python's lxml defaults to XPath 1.0, and most browser and automation tools sit at 1.0 too. If you need 2.0-and-up niceties like case-insensitive matching, you'll often be reaching for workarounds (such as the classic translate() trick) instead of the cleaner modern function.

Tier 1: The Official Specifications (W3C)

When a cheat sheet and your code disagree, the specification wins. These are the authoritative documents from the World Wide Web Consortium. They're dense, but they're the ground truth — and the function reference in particular is genuinely usable as a lookup.

Tier 2: Fast Interactive Cheat Sheets

This is the category I'd actually pin to a corkboard. These pages are example-driven, skimmable, and built for "I just need the syntax" moments.

  • Devhints — XPath — my go-to. It's organized around real expressions: axes, predicates, string functions, and the workarounds you hit in practice (like checking for a class within a space-separated list). Clean, dense, and fast to scan.
  • QuickRef.me — XPath — a similar single-page layout that pairs XPath positioning methods with their CSS-selector equivalents, which is handy if you think in CSS and need to translate.
  • W3Schools — XPath Tutorial — a gentle, table-heavy walkthrough of syntax, axes, operators, and wildcards, with a consistent sample document so the examples build on each other. Good for learning rather than rapid lookup.
  • MDN — XPath reference — Mozilla's documentation covering the axes, functions, and the browser DOM evaluate() API. Reliable and well maintained, with a web-developer slant.

Tier 3: Library and Implementation Docs

XPath rarely runs on its own — it lives inside a library. These docs tell you the dialect, quirks, and integration details of the specific engine you're calling.

  • lxml — XPath and XSLT — the de facto XPath engine for Python (built on libxml2). The docs cover the xpath() method, XPath variables, and the differences between querying a tree versus an element. Essential reading if you scrape with Python.
  • Scrapy / Parsel selectors — Scrapy's selector layer (powered by Parsel) wraps lxml and adds a few conveniences; the docs explain how XPath behaves inside a Scrapy spider.
  • Selenium — locator strategies — how XPath fits alongside CSS, ID, and other locators when driving a browser for testing.
  • Playwright — other locators (XPath) — Playwright supports XPath but explicitly recommends user-facing locators first; worth reading for the why as much as the how.

Tier 4: Test Expressions Live in the Browser

The fastest feedback loop for an XPath expression is your own browser's developer tools — no extra install required.

  • In Chrome or Firefox DevTools, open the console and run $x("//h1"). It evaluates the expression against the current page and returns the matching nodes, so you can iterate on a selector before pasting it into your scraper or test.
  • The Elements panel also lets you right-click a node and copy an XPath, though auto-generated paths tend to be brittle — treat them as a starting point, not a final answer.
  • For a sandbox outside your own pages, online XPath testers (paste in HTML/XML, type an expression, see the matches) are useful when you want a controlled document to experiment against.

A Small Built-In Cheat Sheet

If you only memorize a handful of patterns, make it these.

Selecting nodes

code
/bookstore/book        Direct children from the root
//book                 Any 'book' element, anywhere
//book/title           'title' children of any 'book'
//*                    Every element
//@lang                Every 'lang' attribute
text()                 The text content of a node

Predicates (filters in square brackets)

code
//book[1]                       First 'book' (indexing starts at 1, not 0)
//book[last()]                  Last 'book'
//book[position() <= 3]         First three
//book[@category="web"]         Attribute equals a value
//book[price > 35]              Numeric comparison
//div[@id="head" and @class="x"]  Combine conditions

Useful functions

code
contains(@class, "card")        Substring / partial-class match
starts-with(@href, "https")     Prefix match
not(@disabled)                  Negation
count(//li)                     Count matching nodes
normalize-space(text())         Trim and collapse whitespace

Axes (relationships beyond parent/child)

code
//li/following-sibling::li      Later siblings
//span/ancestor::div            Enclosing 'div' ancestors
//td/parent::tr                 The parent row
//*[@id="x"]/descendant::a      All descendant links

The case-insensitive workaround (XPath 1.0)

When you can't use lower-case() because you're stuck on 1.0:

code
//a[contains(translate(., "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
                           "abcdefghijklmnopqrstuvwxyz"), "buy now")]

It's clunky, but it's the standard 1.0 way to match text regardless of case.

Final Thoughts

The references above cover the full range — from the formal W3C specs you cite when you need to be certain, to the one-page cheat sheets you keep open while you work, to the library docs that explain your specific engine's behavior. If you're scraping or testing, bookmark Devhints and the lxml docs; if you're writing transformations or working with JSON, keep the W3C functions reference within reach.

And whenever an expression behaves strangely, the first question to ask is almost always the same one: which version of XPath am I actually running?

Disclaimer: I'm not affiliated with any of the projects or sites linked above — these are simply the references I find genuinely useful.