Documentation Python quickstart Blog Free tools Enterprise solutions hello@quanticdata.ioLog in

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

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.
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.

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.

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

EngineCSS selectorXPathFaster
Chromium 156, inside the page (querySelectorAll vs document.evaluate), mean of 5,000 runs2.1 µs18.0 µsCSS
lxml 6.1.1, compiled, median of 500 runs33 µs25 µsXPath
lxml 6.1.1, not compiled61 µs31 µsXPath
Playwright 1.64.1 locator count(), median of 200 runs1.14 ms0.84 msabout 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).

GoalCSS selectorXPath
By tagarticle//article
By classp.price_color//p[contains(@class, "price_color")]
By id#product_gallery//*[@id="product_gallery"]
Child, descendantarticle > h3 a//article/h3//a
By attributea[href*="page-2"]//a[contains(@href, "page-2")]
Nth elementol.row > li:nth-child(2)(//ol[@class="row"]/li)[2]
Attribute value::attr(href) in Scrapy/parsel/@href
By textnot possible in standard CSS//a[contains(text(), "Travel")]
Parent of a matchnot 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.

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.

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.
  • 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.
  • Beautiful Soup: CSS only, through select(); for XPath, parse with lxml instead. See Beautiful Soup 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.

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, and our web scraping API returns rendered pages when you would rather not run browsers.

Sources & further reading

FAQ

Quick answers on css selector vs xpath.

Something else? Ask us

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.

Related reading