DevTools Logo

Playwright Selector Builder

Playwright & Puppeteer Selector Builder

Paste HTML and copy uniqueness-verified locators.

Runs in your browser
Try an example

Input

1 line · 23 chars

Result

Working…

Selector Builder sanitizes pasted HTML, renders it in an isolated preview, and generates candidate locators for whichever element you click. Every candidate is verified against the live preview document, so the badge tells you whether it matches exactly one element, several, or none. Strategies are ranked by resilience: role and accessible name first, then test id, label, text, id/CSS, and finally a minimized structural path with XPath as a fallback. Switch between Playwright and Puppeteer output and copy a single locator or the whole annotated snippet. Nothing is uploaded; scripts in pasted markup are stripped before rendering.

Examples

Input
<button data-testid="save">Save</button>
Output
page.getByRole("button", { name: "Save" })

Role and name is preferred because it mirrors how users find the control.

Input
<label for="e">Email</label><input id="e">
Output
page.getByLabel("Email")

Label association is resolved from for/id and from wrapping labels.

Input
<div><span>Plain</span></div>
Output
body > div > span

With no accessible name or test id, a minimized unique CSS path is the honest fallback.

About this tool

The Playwright & Puppeteer Selector Builder turns a pasted HTML fragment into a shortlist of locators for one clicked element. Rather than guessing from the markup text, it runs each candidate against the rendered preview and reports how many elements actually match, so a "unique" claim is always backed by a live query.

Candidates follow the resilience order used in maintainable test suites: role with accessible name, test id, associated label, visible text, a stable id or attribute selector, and only then a minimized structural CSS path or XPath. Each card explains why it is stable or brittle, so you can make an informed choice instead of copying the first result.

The preview is built from sanitized markup with scripts and navigation vectors removed. Generation, verification, and snippet emission all happen in your browser; the HTML never leaves the tab.

What this tool does not do

  • Accessible name computation is a documented subset of the ARIA algorithm, not a full conformance implementation.
  • XPath verification depends on document.evaluate; where unavailable, XPath cards are marked unverified rather than claiming a match count.
  • Only pasted markup is inspected — live applications, shadow roots closed to the preview, and framework-authored DOM are out of scope.

How to use this tool

  1. Paste HTML

    Paste a fragment or full document into the HTML panel, or load an example chip. Inline scripts and meta refresh tags are stripped before the preview is built.

  2. Select an element

    Click an element in the preview, or choose one from the keyboard-accessible element list. The selected element is outlined and its tag summary is shown as text.

  3. Review ranked strategies

    Each card shows a code snippet, a framework chip, a uniqueness badge (exact-1, matches-N or 0) and a rationale explaining how likely it is to survive DOM changes.

  4. Copy a snippet

    Copy one strategy or the whole annotated snippet. Switch between Playwright and Puppeteer output and configure the test id attribute before copying.

Use cases

Harden recorded tests

Replace nth-child paths recorded by browser extensions with role, label, or test id locators that are verified unique in the same preview.

Design a test-id strategy

See which elements lack a stable hook, then add data-testid attributes where generated CSS paths are the only option.

Puppeteer fallbacks

Get CSS and XPath strategies with idiomatic Puppeteer query syntax when Playwright locators are not available.

Strategy families

FamilyExample output
Role + namepage.getByRole("button", { name: "Submit order" })
Test idpage.getByTestId("checkout-cta")
Labelpage.getByLabel("Email address")
Textpage.getByText("Submit order", { exact: true })
CSS pathmain > form > button[type="submit"]
XPath//button[@type="submit"]

A badge of "exact-1" means one live match in the preview; a duplicate name is reported as matches-N with disambiguation guidance.

Common mistakes

Mistake:Copying a brittle nth-child path when a role strategy is available.

Fix:Prefer the top-ranked card: role and accessible name track what the user sees, while structural paths break when a wrapper is added.

Mistake:Ignoring a matches-N badge.

Fix:A locator matching several elements will fail strict mode at runtime. Use the disambiguation hint, add a test id, or scope the locator to a parent.

Mistake:Changing the test id attribute but copying getByTestId anyway.

Fix:Playwright reads data-testid by default. When you configure another attribute, copy the emitted CSS fallback or set testIdAttribute in your Playwright config.

Frequently asked questions

Which selector should I prefer?

Take the top-ranked strategy that is verified as exact-1. Role with an accessible name is usually best because it reflects what a user sees and survives class or wrapper changes. Test ids and labels are also strong; text is reasonable when copy is stable; structural CSS paths and XPath are last-resort fallbacks.

How is the accessible name computed?

The tool follows the common precedence: aria-labelledby is resolved first, then aria-label, then native semantics such as a label associated with an input, button or link text, and image alt text. Placeholder text is never used as an accessible name; it is offered as a separate placeholder strategy where appropriate.

Are selectors really verified as unique?

Yes. CSS candidates are checked with querySelectorAll against the preview document, and role or text candidates are checked with an equivalent filtered walk. The badge reports the live count: exact-1, matches-N, or 0. Candidates that cannot be verified, such as XPath where document.evaluate is unavailable, are labelled accordingly instead of being given a false badge.

Does it work for Puppeteer?

Yes. Puppeteer mode emits idiomatic single-element queries (page.$ / page.waitForSelector) for CSS and text pseudo-selectors, with a note when a Playwright-only locator family has no direct Puppeteer equivalent.

Can I customize the test id attribute?

Yes. Set the test id attribute field before copying. For Playwright, the emitted snippet includes a note that your config needs testIdAttribute set to the same value; otherwise a CSS attribute selector is provided as a drop-in fallback.

References & standards