RepoPilot

What's the recommended way to select an element by text in Playwright?

About microsoft/playwright · 2 messages · last activity 7/12/2026

Question

What's the recommended way to select an element by text in Playwright?

RepoPilot

Based on the repository data provided, I can see examples in the README that demonstrate text-based selection, though the detailed implementation isn't fully shown in the chunks. From the README excerpt, I can see that Playwright provides a method like getByRole for selecting elements:

In the README example [microsoft/playwright, README]:

test('get started link', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  await page.getByRole('link', { name: 'Get started' }).

This shows that getByRole is a recommended approach for selecting elements by their accessible role and name/text. This is a web-first selector that locates elements based on their semantic meaning rather than implementation details.

However, the complete list of text selection methods and their detailed documentation isn't included in the source chunks provided. Based on what's visible, Playwright appears to support text-based locators, though the full API reference would be found in the official Playwright documentation.

For assertions on text content after selecting an element, the repository shows several matchers like:

  • toHaveText() [packages/playwright/src/matchers/matchers.ts:L392-L410]
  • toContainText() [packages/playwright/src/matchers/matchers.ts:L207-L225]

FOLLOW_UPS:

  • How does toHaveText differ from toContainText?
  • What are Playwright's web-first selector alternatives?
  • How do I wait for text to appear on page?

Want to ask your own question?

Open chat for microsoft/playwright

Public Q&A. Generated by RepoPilot from the actual source of microsoft/playwright. AI answers can be incomplete or stale — verify before relying on them.