Skip to content

Playwright getByRole Not Finding Element Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Playwright getByRole Not Finding Element Fix. We cover key concepts, practical examples, and best practices.

getByRole('button') doesn't find the button you expect — or throws "strict mode violation" because it matches multiple elements.

The Problem

// WRONG — getByRole without name filter
test('click submit', async ({ page }) => {
  await page.getByRole('button').click();
  // Strict mode violation — multiple buttons on the page
});
Error: strict mode violation: locator('button') resolved to 3 elements

getByRole('button') matches all buttons on the page. Without a name filter, it's ambiguous.

Step-by-Step Fix

1. Always use the name option

// RIGHT — getByRole with accessible name
import { test, expect } from '@playwright/test';

test('click submit button', async ({ page }) => {
  await page.goto('https://example.com');

  await page.getByRole('button', { name: 'Submit' }).click();
});

2. Use correct role values

// RIGHT — common accessible roles
test('find elements by role', async ({ page }) => {
  await page.getByRole('heading', { name: 'Welcome' });
  await page.getByRole('link', { name: 'Learn more' });
  await page.getByRole('textbox', { name: 'Email' });
  await page.getByRole('checkbox', { name: 'Agree' });
  await page.getByRole('combobox', { name: 'Country' });
});

3. Use getByRole with exact matching

// RIGHT — exact or partial name match
test('exact vs partial name', async ({ page }) => {
  // Partial match (default) — matches "Submit", "Submit Form", etc.
  await page.getByRole('button', { name: 'Submit' });

  // Exact match
  await page.getByRole('button', { name: 'Submit', exact: true });
});

4. Test accessibility with getByRole

// RIGHT — verify accessible names
test('elements have correct accessible names', async ({ page }) => {
  await page.goto('https://example.com');

  await expect(
    page.getByRole('button', { name: 'Submit' })
  ).toBeVisible();

  await expect(
    page.getByRole('heading', { name: 'User Profile' })
  ).toBeVisible();
});

Expected output:

✓ click submit button (0.8s)
✓ find elements by role (0.6s)
✓ exact vs partial name (0.5s)

Prevention Tips

  • Always provide name option to getByRole
  • Use accessibility roles (button, link, heading, textbox, etc.)
  • Use exact: true for precise name matching
  • Test with accessibility tools to verify roles are correct
  • Prefer getByRole over CSS selectors for robust tests

Common Mistakes with locator role

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What roles does Playwright support?

Playwright supports all WAI-ARIA roles: button, link, heading, textbox, checkbox, radio, combobox, listbox, option, tab, menu, dialog, alert, etc. Use page.getByRole() with any valid ARIA role.

Why does getByRole not find my custom component?

Custom components need proper ARIA attributes. Add role="button" and aria-label="Submit" or use a <button> element. Playwright finds elements by their computed ARIA role, not their HTML tag.

What happens if no name is specified?

Playwright searches for elements with any accessible name. If the element doesn't have an accessible name (no text content, no aria-label, no aria-labelledby), it won't be found.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro