Skip to main content

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.

✓ 6 selector types ✓ click / fill / expect chains ✓ ARIA role dropdown ✓ Quick reference table ✓ Runs in browser — no signup

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.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Playwright Config Generator Playwright E2E Test Generator Playwright E2E Testing Guide Playwright Test Generator Playwright Selector Tester SQL to TypeScript Types TypeScript Discriminated Union Builder TypeScript Playground TypeScript to JavaScript Converter TypeScript tsconfig.json Generator JS Prototype Chain Visualizer Jest Testing Framework Guide Cypress Frontend Testing Guide Neon DB Query Builder Code Tools JavaScript Formatter & Minifier JSON to TypeScript Interface Prettier Config Generator Vitest Guide — Vite-native Testing Zustand Store Builder GraphQL Client Configurator Axios Interceptor Builder Jest Mock Generator Turso Edge DB Explorer AI Unit Test Generator — Pro Tool API Response Mock Generator HTTP API Tester ESLint Config Generator Regex Visualizer Regex Tester Pro Regex Tester Biome Config Generator Vitest Configuration Generator Regex Flags Explainer k6 Load Test Script Generator Jest Coverage Config Generator Bundle Size Estimator JavaScript Memory Leak Detector NPM Bundle Size Estimator AWS CDK Infrastructure as Code Guide Pulumi Modern IaC Guide JSON Schema to Zod Converter Drizzle Schema Generator API Response Mocker Pulumi Stack Config Generator Bundle Size Impact Calculator HTML5 Canvas API Playground Game Loop Calculator Bezier Curve Editor Particle System Config Generator Prompt Injection Tester TanStack Query Config Builder SWR Config Generator Jotai Atom Explorer OpenAI Function Calling Generator API Test Case Generator React Component Generator

Related Articles

Mar 28, 2026 · 11 min read

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.

Mar 30, 2026 · 12 min read

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.

Apr 1, 2026 · 15 min read

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.

Mar 28, 2026 · 11 min read

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.

Mar 28, 2026 · 10 min read

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.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$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.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →