← Back to library
Playwrightselectorsbeginnerv1.20+

getByText — find element by visible text

Locate any element by its visible text content — supports exact strings and regex patterns.

1test('finds elements by their text content', async ({ page }) => {
2  await page.goto('/pricing');
3
4  // Exact string match — finds the element containing exactly this text
5  await page.getByText('Get started for free').click();
6
7  // Regex — useful for dynamic text or case-insensitive matching
8  await expect(page.getByText(/welcome,/i)).toBeVisible();
9
10  // Scope to a specific element type to avoid ambiguous matches
11  await expect(page.getByRole('heading').getByText('Dashboard')).toBeVisible();
12});
#getByText#text content#regex#visible text