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
| 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.
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_SELECTORandBy.XPATH; both found their elements in our run, and evenBy.IDis 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 extensionli.next a::attr(href)returnedpage-2.htmlwithout 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.


