Skip to content

XPath and CSS selector cheat sheet for web scraping

By the ProxyOnly team · 16 Nov 2026 · 5 min read

XPath and CSS selectors side by side, from classes and attributes to contains-text, position, parents and siblings, each one tested on the same HTML with its real output.

Every scraper comes down to selectors: short expressions that point at the parts of a page you want. CSS selectors are shorter and read like the stylesheets they come from. XPath is longer and can do more, such as matching text and walking up to a parent. This cheat sheet puts them side by side and shows what each one returned.

We tested every selector on 16 November 2026 with parsel 1.12.1, the selector library that Scrapy uses, against the small piece of HTML below. Where a CSS and an XPath selector are paired, both returned the same elements.

The test HTML

<div id="shop">
  <ul class="products">
    <li class="item sale" data-sku="A-100"><a href="/p/a100">Blue mug</a><span class="price">$8</span></li>
    <li class="item" data-sku="B-200"><a href="/p/b200">Red mug</a><span class="price">$9</span></li>
    <li class="item new" data-sku="C-300"><a href="https://shop.example/p/c300">Green  teapot</a><span class="price">$25</span></li>
  </ul>
  <p class="note">Ships in 2 days</p>
</div>

The double space in "Green teapot" is deliberate. Real pages are full of stray whitespace, and it breaks exact text matches.

Selecting elements

  • All list items: CSS li, XPath //li.
  • By class: CSS li.sale, XPath //li[contains(concat(' ', normalize-space(@class), ' '), ' sale ')]. The long XPath form matches the whole class name, so sale does not also match wholesale.
  • By id: CSS #shop p, XPath //*[@id='shop']//p.
  • Attribute equals: CSS li[data-sku='B-200'] a, XPath //li[@data-sku='B-200']/a.
  • Attribute starts with: CSS a[href^='https'], XPath //a[starts-with(@href, 'https')].
  • Attribute contains: CSS a[href*='b200'], XPath //a[contains(@href, 'b200')].
  • Direct child: CSS ul > li > span, XPath //ul/li/span.
  • Any descendant: a space in CSS, // in XPath, as in #shop p and //*[@id='shop']//p.
  • Second item: CSS li:nth-child(2) a, XPath (//li)[2]/a.
  • Last item: CSS li:last-child a, XPath (//li)[last()]/a.
  • Not matching: CSS li:not(.sale) a::text returned ['Red mug', 'Green teapot'].

Mind the brackets in XPath positions. (//li)[2] is the second li in the whole document, while //li[2] is every li that is the second child of its parent, which on a page with several lists returns several elements.

Getting text and attributes

  • Text in CSS uses ::text, which is an extension in parsel and Scrapy: li a::text returned ['Blue mug', 'Red mug', 'Green teapot'].
  • Text in XPath uses text(): //li/a/text() returned the same list.
  • An attribute in CSS uses ::attr(href), also an extension: li a::attr(href) returned ['/p/a100', '/p/b200', 'https://shop.example/p/c300'].
  • An attribute in XPath uses @href: //li/a/@href returned the same list.

In the browser's developer tools and in BeautifulSoup, CSS has no ::text or ::attr(). There you select the element and read its text or attribute in code.

Matching by text: XPath's strength

Standard CSS cannot select an element by its text. XPath can.

  • Contains text: //a[contains(text(), 'mug')]/text() returned ['Blue mug', 'Red mug'].
  • Exact text: //a[text()='Red mug']/@href returned /p/b200.
  • Text with stray whitespace: //a[normalize-space()='Green teapot']/@href returned https://shop.example/p/c300. An exact match on text() fails here, because the raw text is 'Green teapot' with two spaces.
  • Case-insensitive contains: XPath 1.0 has no lower-case function, so use translate: //a[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'blue')]/text() returned Blue mug.
  • A CSS shortcut that is not standard: parsel's CSS supports :contains(), and li:contains('teapot') a::text returned ['Green teapot']. It will not work in a browser.

The search people type as "xpath like text" usually means one of the first three: contains() for a partial match, text()= for an exact one, and normalize-space() when spacing gets in the way.

Moving around the tree

Only XPath can step upwards or to an earlier sibling.

  • Parent: //a[text()='Red mug']/../@data-sku returned B-200.
  • Next sibling: //a[text()='Red mug']/following-sibling::span/text() returned $9.
  • Previous sibling: //span[text()='$25']/preceding-sibling::a/text() returned Green teapot.
  • Any ancestor: //span[text()='$9']/ancestor::div/@id returned shop.

These are how you find a price next to a label, or the product card that contains a particular word.

Counting and comparing

  • Count: count(//li) returned 3.0.
  • Compare a number inside text: //li[number(substring-after(span, '$')) > 8]/a/text() returned ['Red mug', 'Green teapot'], the products priced above 8 dollars.

Usually it is cleaner to extract the text and compare numbers in your own code. These forms are handy for a quick filter.

Test a selector in the browser first

Before you put a selector in code, try it on the live page. In Chrome's developer tools console, $$(selector) returns the elements that match a CSS selector, and $x(path) returns the elements that match an XPath expression, as Chrome's console utilities reference describes. If the console finds nothing, your script will find nothing either.

One difference catches people out. The browser console runs on the page after JavaScript has changed it, while a script that downloads HTML sees only what the server sent. If a selector works in the console and fails in Requests or Scrapy, open the page source rather than the inspector, and check whether the element is there at all. If it is not, the page builds it with JavaScript, and you need either the JSON request behind it or a browser tool.

Using selectors in other tools

  • parsel and Scrapy: response.css() and response.xpath(), with ::text and ::attr() for CSS, as in this cheat sheet.
  • BeautifulSoup: select() and select_one() take CSS. For XPath, parse with lxml instead.
  • Playwright: page.locator() accepts CSS by default and XPath with an xpath= prefix. Playwright's locator documentation recommends user-visible locators, such as text and roles, over XPath where you can.
  • The browser and JavaScript: document.querySelectorAll() takes CSS, and document.evaluate() takes XPath.

Which one to use

  • Use CSS for most work. It is shorter, works in BeautifulSoup, Scrapy, Playwright and the browser console, and covers classes, ids and attributes.
  • Use XPath when you need to match on text, step up to a parent or across to a sibling, or use position across the whole document.
  • Avoid long chains copied from developer tools, such as /html/body/div[3]/div[2]/ul/li[1]. They break when the layout changes, while a selector built on a class, an id or a data- attribute usually survives.

To use these in practice, our BeautifulSoup tutorial shows CSS selectors in a full scraper, and the Scrapy tutorial uses both kinds. For pages that need a browser, browser automation behind a proxy covers Playwright, and what is web scraping explains where selectors fit in the whole process.

Try it yourself

Create an account, add funds from US$1 and order a proxy in the country you need.

Keep reading