Skip to main content
Free plan, no cardStart free

EngineeringOriginal research

XPath vs CSS Selectors for Test Automation: Measured on 10,000 Elements, and Which Ones Survive a Changed Page

The usual advice is "CSS is faster, XPath is more powerful". Both are true and neither is the point. We timed both on 10,000 elements in three engines, then changed the page and watched which selectors still found the right element.

Summarise with AI

Opens a new chat with only this request: Summarise the key points of this article: https://dokimore.com/blog/xpath-vs-css-selectors-for-test-automation

<0.1 msA CSS id lookup on a 10,000-item page, median of 200 runs, in all three engines
3–9.5 msThe same lookup by XPath (//*[@id="…"]): Firefox 3, WebKit 6, Chromium 9.5
111 msWebKit’s median for the positional CSS selector li:nth-child(7777) > button; XPath li[7777] took 2 ms
4 of 5Selector shapes that still found the same element after a row was inserted above it; the positional shape was the one that did not, in CSS and in XPath alike

In short

  • Speed: CSS wins in the page, by milliseconds that no test will notice. Median of 200 runs on a 10,000-item list: an id lookup took under 0.1 ms by CSS and 3 to 9.5 ms by XPath across Chromium 151, Firefox 153 and WebKit 26.5; a data-testid lookup under 0.1 ms against 3 to 7 ms. The one exception runs the other way: WebKit took 111 ms for li:nth-child(7777) > button and 2 ms for the XPath li[7777]/button.
  • Power: XPath selects by text (//li[.//span[normalize-space()="Item 7777"]]/button) and climbs to a parent; CSS does neither. Playwright gives you the text case without XPath, through getByRole and getByText, at 57 to 368 ms per round trip in our run against 10 to 13 ms for the XPath, so the cost of the readable form is real but still a fraction of a page load.
  • Durability is what matters: after one row was inserted at the top of the list, selectors by id, data-testid, class and text still found the same button in all three engines; both positional selectors, CSS nth-child and XPath li[N], found the row above it. The language is not the risk. Position is.

About this article

Format
Original research: numbers we measured, with the method and the intervals.
Written for
SDETs and automation engineers · Developers who own their tests · QA leads and test managers
Where it applies
Every market. The rules that differ by country are on the country pages.

Every test suite has the argument. One side says XPath is slow and unreadable; the other says CSS cannot find a button by its text or climb to the row that contains it. Both are repeating something true and arguing about the wrong thing, because the cost of a locator is paid when the page changes, not when the page is queried.

So we measured both parts. A fixture page with a 10,000-item list, each item carrying an id, a class, a data-testid, a text span and a button, was loaded in Playwright 1.62.1 under Chromium 151, Firefox 153 and WebKit 26.5. Five selector shapes were timed in the page 200 times each and through Playwright’s locator round trip 30 times each. Then one row was inserted at the top of the list and every selector was asked again which element it found.

In the page: querySelector against document.evaluate

CSS lookups by id, test id or class are effectively free in every engine; the browser keeps indexes for them. XPath walks, so it costs a few milliseconds on a page this large, with Chromium the slowest of the three for the id and test-id forms. The one result that surprised us is WebKit’s 111 ms for the positional CSS selector against 2 ms for the equivalent XPath: nth-child on a 10,000-child list is expensive in that engine. None of these numbers decides anything for a test, where a single page load costs hundreds of milliseconds, but they do dispose of the idea that XPath is too slow to use.

Median of 200 runs, milliseconds, on 10,000 list items; the target is item 7777. Firefox rounds performance.now() to whole milliseconds, which is why its small values read 0 and 1
Selector shapeCSS: Chromium / Firefox / WebKitXPath: Chromium / Firefox / WebKit
By id: #row-7777 vs //*[@id="row-7777"]0.0 / 0 / 09.5 / 3 / 6
By test id: [data-testid="row-7777"] vs //*[@data-testid="row-7777"]0.1 / 0 / 06.5 / 3 / 7
Class then child: li.row-7777 > button vs //li[contains(concat(" ", normalize-space(@class), " "), " row-7777 ")]/button0.1 / 0 / 05.4 / 7 / 9
Position: #list > li:nth-child(7777) > button vs //ul[@id="list"]/li[7777]/button0.3 / 1 / 1111.9 / 1 / 2
By text (XPath only): //li[.//span[normalize-space()="Item 7777"]]/button—5.3 / 4 / 4

Through Playwright: the locator round trip

Through the driver the picture changes, because Playwright does more than call querySelector: it resolves the locator in its own injected script, checks it is unique where required and waits for actionability. The positional CSS form is slow in all three engines here, 187 to 410 ms, where the XPath form is 8 to 10 ms. The readable text locators, has-text and getByRole with hasText, cost 57 to 368 ms, with Firefox the slowest. These are the real costs a test pays per action, and even the worst is a third of a second, which is why the advice to prefer role and text locators survives a performance measurement: the readability is paid for in milliseconds, once per action.

Median of 30 runs of locator(...).evaluate(el => el.tagName), milliseconds, driver to browser and back
Selector shapeCSS: Chromium / Firefox / WebKitXPath: Chromium / Firefox / WebKit
By id13 / 40.5 / 1411 / 10 / 17
By test id16 / 39 / 1310 / 11 / 16.5
Class then child24.5 / 86 / 2411 / 28 / 16
Position (nth-child vs li[N])187 / 410.5 / 390.58 / 10 / 9
By text: XPath vs li:has-text("Item 7777") >> button vs locator('li', { hasText }).getByRole('button')has-text 73 / 387.5 / 58.5; getByRole 65 / 367.5 / 5713 / 12 / 10.5

The part that matters: insert one row and ask again

One inserted row, the kind of change a promotion banner or a sticky header makes every week, and both positional selectors silently pointed at a different element. Not a failure: a wrong click. The id, test-id, class and text forms were unaffected in both languages. The lesson is not "use CSS" or "use XPath"; it is that any selector encoding where an element sits in its parent is a bet that nothing will ever be inserted above it, and that bet loses.

After a new <li class="row promo"> was prepended to the list, the element each selector found (the target is row-7777); identical in Chromium, Firefox and WebKit
Selector shapeCSS foundXPath found
By idrow-7777row-7777
By test idrow-7777row-7777
Class then childrow-7777row-7777
Positionrow-7776 (wrong)row-7776 (wrong)
By text—row-7777

In the product

What to use, in order

  • A role with an accessible name (getByRole('button', { name: 'Pay' })) or a label: it reads like the user and survives restyling. Cost measured here: 57 to 368 ms per action, depending on the engine.
  • A test id (data-testid) where a role is not unique: free in CSS, a few milliseconds in XPath, unaffected by an inserted row.
  • An id or a class that means something; a class named for styling (col-md-4) will change with the next redesign.
  • XPath when you need text matching outside Playwright, or a parent: //tr[td[normalize-space()="Order 42"]]//button. Readable if kept short.
  • Never a position (nth-child, li[3], a long chain of div > div > div) unless the position is the thing under test, such as "the first row is the newest".

How Dokimore stores locators

The recorder keeps several locators per element at record time, in both languages, in xpath_details.json, one of the seven files a session produces. When the first stops matching, the others try to rescue the step and the run detail shows the original, the replacement and its confidence. The table above is why: the stored test-id or text locator is the one that still points at the right element after the page changed, and the positional one is the one that needs rescuing. The selector tester runs the same two languages against pasted HTML.

What to do next

  1. Search your suite for nth-child, nth-of-type and XPath indexes like li[3]; each one is a wrong click waiting for an inserted row.
  2. Move the elements a test acts on to role, label or test-id locators, and keep XPath for text and parent navigation outside Playwright.
  3. Ask the front-end team for data-testid on the elements the suite depends on; it is free to query and it is the attribute least likely to change.
  4. Paste a page and a selector into the selector tester before adding the selector to a page object, and read the path it reports.
  5. Reproduce the measurement on your own largest page with the linked script; the numbers above are for a 10,000-item list and your page is different.

Questions people ask

Is XPath slower than CSS selectors?

Yes, in the page: an id lookup was under 0.1 ms by CSS and 3 to 9.5 ms by XPath on a 10,000-item list across Chromium, Firefox and WebKit. The difference is milliseconds and no test notices it. The one reversal was WebKit’s 111 ms for a positional CSS selector against 2 ms for the equivalent XPath.

When should I use XPath instead of CSS?

When you need to match by text or move to a parent element, which CSS cannot do. Inside Playwright, getByRole and getByText cover the text case without XPath at 57 to 368 ms per action in our measurement; outside it, a short XPath like //tr[td[normalize-space()="Order 42"]]//button is the right tool.

Which selectors break when the page changes?

The positional ones, in either language. After one row was inserted above the target, CSS nth-child and XPath li[N] both pointed at the row above it, in all three engines, while id, data-testid, class and text selectors still found the right element. Position is the risk, not the language.

What is the most reliable locator strategy in Playwright?

A role with its accessible name, then a label, then a test id, then a meaningful id or class, with XPath for text or parent navigation and positions only when the position is what the test is about. That order follows both what Playwright recommends and what survived the change in our measurement.

How we measured this

  • Run on 2026-11-04 with Playwright 1.62.1 and its pinned builds (Chromium 151.0.7922.34, Firefox 153.0, WebKit 26.5), networking disabled, fixture on 127.0.0.1. Scripts and raw output: docs/marketing/keyword-research/research/playwright-guides-2026-11-04 (e4-selectors.js, out/e4-selectors.json).
  • The page: one <ul> of 10,000 <li> items, each with an id, two classes, a data-testid, a <span> of text and a <button>; the target is item 7777. In-page timings call document.querySelector and document.evaluate (FIRST_ORDERED_NODE_TYPE) 200 times each and report the median and the 95th percentile of performance.now() differences; Firefox coarsens performance.now() to 1 ms, so its small values read as 0 or 1. Locator timings wrap page.locator(selector).evaluate() in Date.now() 30 times and report the median; they include the driver round trip and Playwright’s own resolution and checks.
  • The robustness check records which <li> each selector resolves to, prepends one <li class="row promo"> to the list with page.evaluate, and records again. One run per engine; the table reports the engines separately because they are the result, not a sample.
  • Dokimore’s behaviour is the product code on the article date: several locators per element are stored at record time and the run detail shows a rescue with its strategy and confidence.

Sources

  1. Playwright documentation, Locators: role, text and test-id locators recommended; CSS and XPath tied to DOM structure break when it changes read
  2. MDN, document.evaluate(): the XPath API the in-page measurement called, and the XPathResult types read
  3. Playwright documentation, Browsers: each release pins its own browser builds read

See it on your own project

Every new account starts a 14-day Solo Pro trial, then 14 days on the Free plan. No card. Record one flow and measure the difference on your own numbers.

  • Free plan, no card
  • 14-day Solo Pro trial
  • Demo replies within one business day
  • Data hosted in India