Skip to content

Menu

Playwright Selector Builder

Build optimal Playwright selectors

Runs entirely in your browserTesting & QAFree, no sign-up
Loading tool

01What is Playwright Selector Builder?

Playwright's own docs on locators recommend user-facing locators such as getByRole and getByLabel over CSS and XPath. The reason is simple: tests that find elements the way a user does break less when the markup is restyled. This builder applies that ranking to an HTML snippet you paste in. The snippet is parsed with the browser's DOMParser. The tool then picks one target: the first button, link with an href, visible input, select, textarea, image, or element with a role or data-testid. If none exists, it falls back to the first element. That is why a <label> before an <input>, or a <nav> around a link, doesn't take over the result. Next comes the role. An explicit role attribute wins. Otherwise the tool maps the tag to its implicit ARIA role. A link needs an href, inputs become textbox, searchbox, spinbutton, slider, checkbox or radio by type, and <select> is combobox or listbox. Headings h1 to h6 get a level option. Password and file inputs have no role Playwright can match, so the first suggestion for them is getByLabel. The accessible name is a simplified version of the browser algorithm. It checks aria-labelledby, aria-label, a <label for> or wrapping label, alt text, the element's text, then title or placeholder. Candidates come out in priority order: getByRole with the name, getByLabel, getByPlaceholder, getByText or getByAltText, getByTitle, getByTestId, a CSS #id, a tag.class selector, a [name] selector, and an XPath text match as the last resort. The first one is marked Recommended. For example, a button with aria-label="Submit form" and visible text Submit produces getByRole('button', { name: 'Submit form' }) first. The aria-label replaces the text as its name, yet getByText('Submit') still appears lower down. Each line ends with an action that fits the element. Text fields get .fill('value'), checkboxes and radios get .check(), selects get .selectOption() and everything else gets .click(). Quotes are escaped, so a link reading Bob's orders yields valid code. A few limits to keep in mind. Only one element per snippet is analysed. The name is worked out from the snippet alone, so labels elsewhere on the page or names set by script are invisible to it. And nothing here checks that a locator matches exactly one element on the live page. Your HTML is parsed in the tab and never uploaded.

QA automation engineers and front-end developers who write Playwright end-to-end tests and want role-based locators instead of brittle CSS. The tool runs entirely in your browser, so your data never leaves your device.

02How to use Playwright Selector Builder

  1. Paste an element's outer HTML into HTML Snippet, or load the Button Element, Input with Label or Navigation Link example.
  2. Click Generate Selectors.
  3. Review the Selectors (Priority Order) list. The first entry is marked Recommended.
  4. Copy a single line with its copy icon, or click Copy All for every option.
  5. Replace the placeholder 'value' in .fill() or .selectOption() with your real test data.

03Worked examples

Input: <button class="btn-primary" data-testid="submit-btn" aria-label="Submit form">Submit</button>
Result: await page.getByRole('button', { name: 'Submit form' }).click(); await page.getByText('Submit').click(); await page.getByTestId('submit-btn').click(); await page.locator('button.btn-primary').click(); await page.locator('//button[normalize-space()="Submit"]').click();
aria-label overrides the visible text as the accessible name, so getByRole uses 'Submit form' while getByText still matches 'Submit'.
Input: <label for="email">Email Address</label> <input id="email" type="email" name="email" placeholder="Enter email" data-testid="email-input" />
Result: await page.getByRole('textbox', { name: 'Email Address' }).fill('value'); await page.getByLabel('Email Address').fill('value'); await page.getByPlaceholder('Enter email').fill('value'); await page.getByTestId('email-input').fill('value'); await page.locator('#email').fill('value'); await page.locator('[name="email"]').fill('value');
For an input the accessible name comes from its <label for>, and because it is a fillable textbox every line ends in .fill('value').
Input: <label><input type="checkbox" name="terms"> I accept the terms</label>
Result: await page.getByRole('checkbox', { name: 'I accept the terms' }).check(); await page.getByLabel('I accept the terms').check(); await page.locator('[name="terms"]').check();
A checkbox wrapped in its label gets its name from the label text and the generated action is .check() instead of .click().
Input: <select id="country" aria-label="Country"><option>US</option></select>
Result: await page.getByRole('combobox', { name: 'Country' }).selectOption('value'); await page.getByLabel('Country').selectOption('value'); await page.locator('#country').selectOption('value');
A single-select <select> has the implicit role combobox, so the tool generates .selectOption('value').
Input: <h2 title="Section">Pricing plans</h2>
Result: await page.getByRole('heading', { name: 'Pricing plans', level: 2 }).click(); await page.getByText('Pricing plans').click(); await page.getByTitle('Section').click(); await page.locator('//h2[normalize-space()="Pricing plans"]').click();
Headings get a level option from the tag (h2 gives level: 2), and the XPath fallback is listed last.
Input: <label for="pw">Password</label><input id="pw" type="password" name="password">
Result: await page.getByLabel('Password').fill('value'); await page.locator('#pw').fill('value'); await page.locator('[name="password"]').fill('value');
Password inputs have no ARIA role, so there is no getByRole line and getByLabel becomes the recommended locator.

04Common use cases

  • Writing a first Playwright test for a login form copied from the browser's Elements panel
  • Replacing brittle CSS class selectors in an existing suite with role-based locators
  • Showing front-end developers which attributes (aria-label, a <label>, data-testid) would make a component easier to test
  • Converting Cypress or Selenium selectors into Playwright locator calls during a migration
  • Checking quickly whether a custom checkbox or select exposes a usable role before writing the test
  • Teaching a new QA engineer how Playwright's locator priority works on real markup

05Tips

  • Paste one control at a time, together with its <label> if it has one. With several buttons or inputs in a snippet, only the first is analysed.
  • If no getByRole line appears, the markup probably lacks a role or name. Fixing that in the component often helps screen reader users too.
  • getByText matches a substring and ignores case by default. Add { exact: true } when a page has similar labels such as Save and Save draft.
  • Run npx playwright codegen or the Playwright Inspector on the live page to confirm a locator hits exactly one element. Strict mode fails on multiple matches.

06Related concepts

Playwright locators, getByRole, Accessible name, End-to-end testing, data-testid.

Frequently Asked Questions

What is Playwright Selector Builder?

Playwright Selector Builder is a free, browser-based developer tool on AIDToolStack. Build optimal Playwright selectors It requires no installation or sign-up and works entirely in your browser.

Is Playwright Selector Builder free to use?

Yes. No sign-up, no fees and no usage cap.

Does Playwright Selector Builder store my data?

No. It runs in your browser; nothing is uploaded.

Cheat sheet

Reviews and commentsShow

Reviews

Sign in to leave a review.

Loading reviews…

Comments

Sign in to leave a comment.

Loading comments…

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe