# CSS Selector vs XPath: Speed, Measured

> CSS selector vs XPath, measured in lxml, Chromium and Playwright on one page: speed, syntax side by side, and the jobs only XPath can do. Python code.

[Home](https://quanticdata.io/) / [Blog](https://quanticdata.io/blog/) / CSS Selector vs XPath: Speed, Measured

# CSS selector vs XPath: which is faster and which can do more, measured in three engines

Web scraping · Nov 20, 2026 · 5 min read · By [Aldo Morese](https://quanticdata.io/about/), founder of QuanticData

![CSS selectors and XPath timed on the same product page in three engines: inside Chromium querySelectorAll took 2.1 microseconds against 18.0 for XPath, in lxml compiled XPath took 25 against 33 for compiled CSS, and Playwright locators took about a millisecond either way; the card lists what only XPath can do, match text and climb to a parent.](https://quanticdata.io/blog/css-selector-vs-xpath/hero.webp?v=20261120h)

On this page

- [Speed: it depends on the engine](https://quanticdata.io/blog/css-selector-vs-xpath/#speed-it-depends-on-the-engine)
- [Syntax side by side](https://quanticdata.io/blog/css-selector-vs-xpath/#syntax-side-by-side)
- [What only XPath can do](https://quanticdata.io/blog/css-selector-vs-xpath/#what-only-xpath-can-do)
- [Where XPath trips people up](https://quanticdata.io/blog/css-selector-vs-xpath/#where-xpath-trips-people-up)
- [In Selenium, Playwright, Beautiful Soup and Scrapy](https://quanticdata.io/blog/css-selector-vs-xpath/#in-selenium-playwright-beautiful-soup-and-scrapy)
- [A rule you can keep](https://quanticdata.io/blog/css-selector-vs-xpath/#a-rule-you-can-keep)

CSS selectors vs XPath: use CSS selectors by default, because they are shorter, read like the page's own stylesheet and were fastest inside the browser in our test. Use XPath when you need to match an element by its text or move from a child up to its parent or a sibling, which CSS selectors cannot do. Both work in Selenium, Playwright, lxml and Scrapy.

**Run it on the free credit.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-lead&utm_content=css-selector-vs-xpath) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

To settle the speed question with numbers, on 20 November 2026 we ran the same query, the 20 book links of a books.toscrape.com catalogue page (a public practice shop, fetched through a QuanticData mobile proxy), in three engines from Python 3.12.10.

## Speed: it depends on the engine

| Engine | CSS selector | XPath | Faster |
| --- | --- | --- | --- |
| Chromium 156, inside the page (querySelectorAll vs document.evaluate), mean of 5,000 runs | 2.1 µs | 18.0 µs | CSS |
| lxml 6.1.1, compiled, median of 500 runs | 33 µs | 25 µs | XPath |
| lxml 6.1.1, not compiled | 61 µs | 31 µs | XPath |
| Playwright 1.64.1 locator count(), median of 200 runs | 1.14 ms | 0.84 ms | about the same |

Browsers have a native CSS engine, so CSS won inside Chromium by a wide margin. lxml is an XPath engine: its CSS support translates each selector into XPath first, and the printed translation of our selector starts with `descendant-or-self::article[@class and contains(@class, ...`. That translation is why CSS came second there. Through Playwright, both took around a millisecond, because each call is a round trip from Python to the browser and the matching itself is a small part of it.

The practical conclusion: on any real job the download of the page takes hundreds of milliseconds, so a difference of microseconds in the selector will not show. Choose for readability and capability, not speed.

A word on method. The Chromium figures are means measured with `performance.now()` inside the page over 5,000 calls each, so they exclude any Python overhead. The lxml and Playwright figures are medians of timed calls from Python on the already parsed or already loaded page. Each engine ran the selector for the same 20 links, and every run returned 20 nodes, so the comparison is like for like.

## Syntax side by side

Each pair below returned the same elements in lxml on our test pages (the id row on a product page).

| Goal | CSS selector | XPath |
| --- | --- | --- |
| By tag | `article` | `//article` |
| By class | `p.price_color` | `//p[contains(@class, "price_color")]` |
| By id | `#product_gallery` | `//*[@id="product_gallery"]` |
| Child, descendant | `article > h3 a` | `//article/h3//a` |
| By attribute | `a[href*="page-2"]` | `//a[contains(@href, "page-2")]` |
| Nth element | `ol.row > li:nth-child(2)` | `(//ol[@class="row"]/li)[2]` |
| Attribute value | `::attr(href)` in Scrapy/parsel | `/@href` |
| By text | not possible in standard CSS | `//a[contains(text(), "Travel")]` |
| Parent of a match | not possible to select upward | `//p[@class="price_color"]/ancestor::article` |

## What only XPath can do

Two jobs come up constantly in scraping. Find a link by its words: `//a[contains(text(), "Travel")]/@href` returned `category/books/travel_2/index.html` on our page. And go from a known value up to its container: starting from the price `£51.77`, `ancestor::article` led to the book "A Light in the Attic". A CSS selector can only move down and sideways, so it cannot express the second query at all. Playwright adds its own text filters on top of CSS (`page.locator("a", has_text="Travel")` found the same link), which removes the first reason for many Playwright users, but not the second.

**Try it on your own targets.** Every QuanticData account gets $2 of free API usage per month.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-inline&utm_content=css-selector-vs-xpath) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

## Where XPath trips people up

Classes. `.star-rating` in CSS matches an element whose class list contains star-rating. `//p[@class="star-rating"]` in XPath compares the whole attribute string, and on our page, where the class is `star-rating Three`, it matched 0 elements against 20 with `contains(@class, "star-rating")`. When most of your selectors are about classes, CSS is simply less error-prone. The full list of XPath expressions we tested is in [lxml XPath in Python](https://quanticdata.io/blog/lxml-xpath-python/).

## In Selenium, Playwright, Beautiful Soup and Scrapy

- **Selenium**: `By.CSS_SELECTOR` and `By.XPATH`; both found their elements in our run, and even `By.ID` is sent to the browser as a CSS selector. Details in [Selenium find_element](https://quanticdata.io/blog/selenium-find-element/).
- **Playwright**: a plain string is CSS, `xpath=` in front makes it XPath; both returned the same 20 links. Pair either with the waits in [Playwright waits](https://quanticdata.io/blog/playwright-wait-for-page-load/).
- **Beautiful Soup**: CSS only, through `select()`; for XPath, parse with lxml instead. See [Beautiful Soup web scraping](https://quanticdata.io/blog/beautifulsoup-web-scraping/).
- **Scrapy and parsel**: both, with `.css()` and `.xpath()` on the same response, and they can be chained: `sel.css("article.product_pod").xpath(".//h3/a/@title").get()` returned "A Light in the Attic", and parsel's CSS extension `li.next a::attr(href)` returned `page-2.html` without any XPath.

## A rule you can keep

Write CSS for anything you can describe by tag, class, id or attribute. Switch to XPath for the query that needs text or a step upward, and do not feel you must convert the rest. Libraries that support both let you mix them in one scraper. Which library to use in the first place is covered in [Python web scraping libraries](https://quanticdata.io/blog/python-web-scraping-libraries/).

Selectors only work on the HTML you receive. When a site sends a block page or an empty shell, both kinds return nothing, and the fix is in how you fetch: our test pages came through QuanticData [mobile proxies](https://quanticdata.io/mobile-proxies/), and our [web scraping API](https://quanticdata.io/web-scraping-api/) returns rendered pages when you would rather not run browsers.

### Sources & further reading

- [W3C: Selectors Level 4](https://www.w3.org/TR/selectors-4/)
- [W3C: XML Path Language (XPath) 1.0](https://www.w3.org/TR/1999/REC-xpath-19991116/)
- [lxml: cssselect](https://lxml.de/cssselect.html)
- [Playwright for Python: other locators (CSS and XPath)](https://playwright.dev/python/docs/other-locators)
- [MDN: Document.querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll)

## FAQ

Quick answers on css selector vs xpath.

[Something else? Ask us](mailto:hello@quanticdata.io)

### Is CSS selector faster than XPath?

In the browser, yes: in Chromium querySelectorAll took 2.1 µs against 18.0 µs for XPath on our page. In lxml it was the other way round, 25 µs for compiled XPath against 33 µs for compiled CSS. In Playwright both took about a millisecond.

### Should I use CSS selector or XPath in Selenium?

CSS selectors for most elements, because they are shorter and handle classes cleanly. XPath when you must find an element by its text or move up to a parent; both work with `find_element`.

### What can XPath do that CSS selectors cannot?

Select by text content and move upward or backward in the tree. On our page, `//a[contains(text(), "Travel")]` found a link by its words and `ancestor::article` climbed from a price to its product.

### Why does my XPath class selector find nothing?

`@class="name"` compares the whole class attribute. If the element has two classes it fails: 0 matches against 20 with `contains(@class, "name")` on our page. The CSS selector `.name` has no such problem.

### Can Beautiful Soup use XPath?

No. Beautiful Soup supports CSS selectors through `select()` and its own `find()` methods. For XPath, parse the same HTML with lxml: `lxml.html.fromstring(html).xpath("...")`, which on our page was also faster.

### Can I use both CSS and XPath in one scraper?

Yes. Scrapy, parsel, lxml, Selenium and Playwright all accept both, so use CSS by default and XPath where it is the only option.

## Selectors need a real page to work on

Mobile and residential proxies for Playwright, Selenium and requests, so the HTML your selectors read is the one real visitors get.

[Start free — $2/month included](https://quanticdata.io/signup/?utm_source=blog&utm_medium=website&utm_campaign=blog-end&utm_content=css-selector-vs-xpath) · [Explore Web Scraping API](https://quanticdata.io/web-scraping-api/)

## Related reading

- [Web Scraping Project Ideas, Started for Real](https://quanticdata.io/blog/web-scraping-project-ideas/): Web scraping · Five web scraping project ideas, each one started for real on 20 November 2026: a price tracker, a library release dashboard, a question analysis, an open-source landscape and a topic timeline, with the code, the first run and what to build next.
- [Retry-After Header: Read It, Wait, Retry](https://quanticdata.io/blog/retry-after-header/): Web scraping · Retry-After tells a client how long to wait before the next request, as a number of seconds or as an HTTP date. Below: both formats, a Python and a Node helper we ran against a real 429 on 20 November 2026 (each waited 10 seconds and then got 200), the rate-limit headers of the QuanticData API, and how one batch job replaced 20 calls.
- [How to Use an API Key: curl, Python and JS](https://quanticdata.io/blog/how-to-use-an-api-key/): Web scraping · An API key is a secret string you send with every request so the service knows which account is calling. Nothing is installed: you add one header. Below: the same call in curl, Python, JavaScript and PowerShell, run on 20 November 2026 against the QuanticData API, the exact 401 messages for the usual mistakes, and how to keep the key out of your code.

---

Source: https://quanticdata.io/blog/css-selector-vs-xpath/ · Site index for AI: https://quanticdata.io/llms.txt
