Playwright getByRole Not Finding Element Fix
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
nameoption togetByRole - Use accessibility roles (button, link, heading, textbox, etc.)
- Use
exact: truefor precise name matching - Test with accessibility tools to verify roles are correct
- Prefer
getByRoleover CSS selectors for robust tests
Common Mistakes with locator role
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro