Playwright Selector Helper
Build Playwright selectors interactively — CSS, role, text, label, placeholder, and testId. Choose a chaining action and get a copy-ready await statement instantly.
Frequently Asked Questions
What is the difference between page.locator() and getByRole()?▼
page.locator() accepts any CSS or XPath selector and is the most flexible — but also the most brittle. getByRole() targets elements by their ARIA role and accessible name, which is how screen readers and keyboard users navigate. Playwright's documentation recommends preferring getByRole, getByLabel, and getByText because they are resilient to structural changes in the HTML and simultaneously verify your app's accessibility.
When should I use getByTestId() vs getByRole()?▼
Use getByRole() whenever the element has a meaningful ARIA role and accessible name — buttons, links, form controls, headings. Fall back to getByTestId() only for elements that have no stable semantic meaning from a user perspective (e.g., a container div or a custom widget with no accessible label). Add data-testid attributes deliberately rather than sprinkling them across every element.
How do I configure a custom testId attribute name?▼
By default Playwright looks for data-testid. You can change this in playwright.config.ts: set use: { testIdAttribute: 'data-cy' } to match Cypress conventions, or any other attribute your codebase uses. getByTestId() will then match that attribute instead. This is useful when migrating test suites from other frameworks.
What does the { exact: true } option do for getByText()?▼
Without exact: true, getByText('Submit') will match any element whose text content includes the word 'Submit' — such as 'Submit your form' or 'Please Submit'. With exact: true, it performs a case-sensitive, full-string match. This is useful when multiple elements on the page contain similar text and you need to target one specific element precisely.
How do I select elements inside a scoped component or iframe?▼
Chain locators with .locator() to scope queries: page.locator('#sidebar').getByRole('link') finds links only inside #sidebar. For iframes, use page.frameLocator('iframe[name=checkout]').getByRole('button', { name: 'Pay' }). This avoids false matches from elsewhere in the page and is much more maintainable than using complex CSS descendant selectors.
What are the best practices for Playwright selectors in CI/CD?▼
Prefer user-visible locators (role, label, placeholder, text) over implementation details (CSS classes, XPath). Avoid nth-child or index-based selectors which break on DOM reorder. Use data-testid for custom components that lack semantic roles. Keep selectors in Page Object Model classes so they are centrally maintained. Enable Playwright's built-in retry and waitFor to handle async rendering without adding manual timeouts.
Related Products
on GumroadTypeScript Quick Reference Toolkit
5 practical reference files for TypeScript developers: cheatsheet, TS vs JS comparison, 20 utility types with real examples, tsconfig best practices template, and 15 common mistakes guide. Print it. Keep it on your desk.
Buy on Gumroad →DevPlaybook Interview Vault
210+ real technical interview questions with model answers, asked at Google, Meta, Amazon, Microsoft, Stripe, and Shopify. Covers Frontend (55), Backend (55), System Design (50), and DSA (50). Available in English and Traditional Chinese.
Buy on Gumroad →Developer Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →You Might Also Need
Playwright Config Generator
Build a playwright.config.ts file visually. Configure browsers, timeouts, retries, reporters, screenshots, video, trace, and web server settings. Copy-ready TypeScript or JavaScript output.
Open free tool →
Playwright E2E Test Generator
Generate Playwright end-to-end test code visually. Add click, fill, expect, and navigate steps, toggle TypeScript/JavaScript, and copy the test code.
Open free tool →
Playwright E2E Testing Guide
Complete Playwright end-to-end testing guide. Learn locators, auto-wait, fixtures, network interception, API testing, Trace Viewer, and multi-browser automation.
Open free tool →
More Free Tools
Related Articles
Playwright vs Cypress vs Puppeteer 2026: Which Testing Tool Should You Choose?
Playwright vs Cypress vs Puppeteer 2026 — compare feature sets, performance, developer experience, parallel testing, CI costs, and browser support. Find the right E2E testing tool for your stack.
Modern TDD in 2026: Test-Driven Development with Vitest, Playwright & AI Copilots
Test-Driven Development has evolved. This practical guide covers modern TDD workflows using Vitest for unit tests, Playwright for E2E, MSW for API mocking, and how AI coding assistants (GitHub Copilot, Claude Code) are changing how we write tests first.
Modern JavaScript Testing 2026: Vitest vs Jest vs Playwright
Complete guide to JavaScript testing in 2026. Compare Vitest vs Jest for unit tests, Playwright vs Cypress for E2E, component testing, coverage tools, and CI/CD integration.
Vitest: The Fast Unit Testing Framework for Vite Projects - 2026 Guide
Complete guide to Vitest in 2026: setup, configuration, mocking, coverage, watch mode, and a detailed comparison with Jest to help you decide which testing framework fits your project.
Vitest vs Jest vs Mocha: Complete Testing Framework Comparison 2026
A comprehensive comparison of Vitest, Jest, and Mocha for JavaScript and TypeScript projects in 2026. Covers speed benchmarks, configuration, TypeScript support, mocking, code coverage, and migration strategies.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →