04 / Testing / css-selector-tester

CSS Selector Tester

Test CSS selectors against HTML and preview matches.

ReadyLocal processing
04 · Workspace

How to use

  1. 01Enter a CSS selector in the CSS selector field, or click an example chip such as ul.items > li:nth-child(2) or [data-testid].
  2. 02Paste the HTML you want to test against into the HTML editor; a sample login page is preloaded.
  3. 03Check the Matched elements panel for the match count and each element's tag, text and outer HTML.
  4. 04Scroll to the Highlighted preview, where every matching element is outlined in a sandboxed render of your HTML with scripts removed.
  5. 05Refine the selector until only the intended elements are highlighted, then use it in your test.

What is this tool?

A CSS selector tester that runs your selector through the browser's native querySelectorAll against pasted HTML, lists every match, and outlines the matches in a live preview. Invalid selectors show the browser's own syntax error. The preview renders in a sandboxed iframe with scripts stripped, and nothing is sent to a server.

CSS selector cheat sheet for Playwright and Cypress locators

The basics: tag (button), class (.btn), id (#login) and attribute ([name='email']). Combine them without spaces to require all conditions on one element, as in button.btn.primary. A space means any descendant (#main-nav a) and > means direct child (ul.items > li). Attribute operators add flexibility: [href^='/docs'] matches a prefix, [href$='.pdf'] a suffix, [class*='active'] a substring and [data-testid] the mere presence of an attribute.

Structural pseudo-classes target position: li:first-child, li:last-child, li:nth-child(2) and li:nth-of-type(odd). Modern browsers also support :not(), :is() and :has(), so form:has(input[type='password']) selects the login form itself.

Worked example: against the preloaded sample, form#login input[data-testid] matches the email and password inputs, a.active matches only the Home link, and ul.items > li:nth-child(2) matches the Beta item.

Writing stable selectors that survive UI changes

Selectors tied to styling break whenever a designer renames a class, and nth-child chains break when content is reordered. The most durable option is a dedicated test attribute such as [data-testid='email'], which Cypress and Playwright documentation both recommend. Next best are semantic attributes like name, type or aria-label, then stable ids.

Use this tester to confirm a selector is unique: if the Matched elements count is greater than one, Playwright's strict mode will throw when you act on that locator, and Cypress will act on several elements at once. Tightening the selector until exactly one element is highlighted avoids both problems.

Native CSS vs framework-specific selector extensions

This tool uses standard browser CSS, so framework-only syntax will report an invalid selector. Playwright extensions such as :has-text(), text= and >> chaining, and jQuery-style pseudo-classes that Cypress accepts, such as :contains() and :visible, are not part of native CSS. Test the CSS portion here and add the framework extension in your code, or switch to an XPath text match when you need to select by content.

The parser also normalizes HTML the way a browser does, so missing tbody elements are inserted and invalid nesting is corrected before your selector runs. That mirrors what your tests see on a real page.

Common use cases

  • Confirming a Playwright locator matches exactly one element before strict mode complains
  • Writing cy.get() selectors for Cypress tests from saved page HTML
  • Comparing data-testid selectors against class-based ones during locator refactoring
  • Visually checking which elements a scraping selector will capture
  • Teaching CSS combinators and pseudo-classes with an instant highlighted preview

FAQ

How do I test a CSS selector against HTML?

Paste the HTML into the editor and type the selector in the CSS selector field. Matches are listed with their outer HTML and outlined in the highlighted preview.

Does it support :has() and :nth-child()?

Yes. The selector runs through your browser's querySelectorAll, so any selector your browser supports, including :has(), :is(), :not() and :nth-child(), works here.

Why is my Playwright text selector invalid here?

Playwright extensions such as :has-text() and text= are not standard CSS. Only native CSS selectors are supported.

Is it safe to paste HTML with scripts?

Yes. Script tags are removed before the preview is rendered, and the preview runs in a fully sandboxed iframe.

Should I use CSS selectors or XPath for test automation?

CSS is usually shorter and faster to read, so prefer it for attribute and structure matching. Use XPath when you need to match on text content or navigate up to a parent element.