How to Fix Playwright “strict mode violation” Errors
When a Playwright test fails with Error: strict mode violation: ... resolved to 3 elements, the locator you used for an action such as click() matched two or more elements on the page. Narrow the locator until it matches exactly one element you want to act on, and the error goes away. You will run into this in E2E tests for admin screens where every row in a list has its own "Save" or "Delete" button, or for pages that include the same menu twice, once for desktop and once for mobile.
I reproduced this error in my test environment and checked the error output, along with whether each fix made the test pass.
The test environment was Ubuntu 24.04, Node.js 22.23.4, and @playwright/test 1.63.0. I wrote the tests in TypeScript and ran them only in headless Chromium. I did not try Firefox, WebKit, or Playwright in other languages such as Python. The test pages were not external sites. I built them inline with page.setContent().
The error output
The test page is a list of draft articles. Each row has a "Save" button, and pressing it shows a message below the list such as "Saved Article A". Below the list there is also a separate "Save and close" button.
// tests/page.ts
export const listPage = (titles: string[]) => `
<h1>下書きの一覧</h1>
<ul>
${titles.map((t) => `<li><span>${t}</span> <button>保存</button></li>`).join('\n ')}
</ul>
<button>保存して閉じる</button>
<p id="status"></p>
<script>
for (const li of document.querySelectorAll('li')) {
li.querySelector('button').addEventListener('click', () => {
document.querySelector('#status').textContent =
li.querySelector('span').textContent + 'を保存しました';
});
}
</script>
`;
export const html = listPage(['記事A', '記事B']);
I wanted to press the "Save" button for Article A on this page, so I wrote the following test in tests/bad.spec.ts.
import { test, expect } from '@playwright/test';
import { html } from './page';
test('保存ボタンを押す', async ({ page }) => {
await page.setContent(html);
await page.getByRole('button', { name: '保存' }).click();
await expect(page.locator('#status')).toHaveText('記事Aを保存しました');
});
I ran npx playwright test tests/bad.spec.ts, and it failed with the following output (exit code 1). I replaced the working directory path with /path/to/app, and I omitted the leading Running 1 test using 1 worker line and everything after Error Context.
✘ 1 tests/bad.spec.ts:4:1 › 保存ボタンを押す (128ms)
1) tests/bad.spec.ts:4:1 › 保存ボタンを押す ──────────────────────────────────────────────────────────────
Error: locator.click: Error: strict mode violation: getByRole('button', { name: '保存' }) resolved to 3 elements:
1) <button>保存</button> aka getByRole('listitem').filter({ hasText: '記事A 保存' }).getByRole('button')
2) <button>保存</button> aka getByRole('listitem').filter({ hasText: '記事B 保存' }).getByRole('button')
3) <button>保存して閉じる</button> aka getByRole('button', { name: '保存して閉じる' })
Call log:
- waiting for getByRole('button', { name: '保存' })
4 | test('保存ボタンを押す', async ({ page }) => {
5 | await page.setContent(html);
> 6 | await page.getByRole('button', { name: '保存' }).click();
| ^
7 | await expect(page.locator('#status')).toHaveText('記事Aを保存しました');
8 | });
9 |
at /path/to/app/tests/bad.spec.ts:6:50
In the line starting with Error:, resolved to 3 elements means the locator matched three elements. Each matched element is listed below it. After aka is another locator that points to the same element. For the first row, that locator is "the button inside the list item filtered by its Article A text". If you are unsure how to narrow the locator, these aka locators are a good starting point.
The screenshot below circles the three matched buttons in pink.

Why the locator must match exactly one element
Playwright locators are strict. The official documentation section on locator strictness says that an action targeting a single element throws an error when the locator matches two or more elements. Playwright does not pick one of them for you.
The third match, "Save and close", is included because of how getByRole() matches name. According to the description of the name option, by default the name is matched case-insensitively and as a substring. "Save and close" contains "Save", so it matches.
Assertions on a single element with expect() also fail the same way. I tried an assertion with exact: true for an exact match, using toBeVisible():
await expect(page.getByRole('button', { name: '保存', exact: true })).toBeVisible();
This excluded "Save and close", but the two per-row "Save" buttons still remained, so the assertion failed:
Error: expect(locator).toBeVisible() failed
Locator: getByRole('button', { name: '保存', exact: true })
Expected: visible
Error: strict mode violation: getByRole('button', { name: '保存', exact: true }) resolved to 2 elements:
1) <button>保存</button> aka getByRole('listitem').filter({ hasText: '記事A 保存' }).getByRole('button')
2) <button>保存</button> aka getByRole('listitem').filter({ hasText: '記事B 保存' }).getByRole('button')
Search within the row you want
When the same button appears in every row, find the row first, then look for the button inside that row. This uses the pattern from the Filter by text section of the official documentation. The locator shown after aka in the error output has the same form.
await page
.getByRole('listitem')
.filter({ hasText: '記事B' })
.getByRole('button', { name: '保存' })
.click();
await expect(page.locator('#status')).toHaveText('記事Bを保存しました');
getByRole('listitem') gets all the rows of the list (<li>), and filter() keeps only the row whose text contains Article B. That row has only one "Save" button, so the click succeeds without an error. This test passed, and the status message for Article B appeared below the list.
If you only need to press a button with a different name, such as "Save and close", the full name is enough to make the locator match one element. A locator built from the full name of the "Save and close" button also passed its click(). Conversely, if a short name like "Save" with a substring match picks up longer button names too, add the exact option to require an exact match.
Using first() hides wrong-row clicks
Adding first(), last(), or nth() lets you choose which of the matched elements to use, so the error goes away. In fact, calling first() on the "Save" button locator pressed Article A's "Save" button and passed.
However, the official documentation does not recommend these methods, because they can press an unintended element when the page changes. I also tried a list where Article B appeared first after reordering. This is the content of tests/first.spec.ts (the import lines are omitted):
const reordered = listPage(['記事B', '記事A']);
test('並び順が変わっても first() で記事Aを押せるか', async ({ page }) => {
await page.setContent(reordered);
console.log(await page.getByRole('listitem').allTextContents());
await page.getByRole('button', { name: '保存', exact: true }).first().click();
await expect(page.locator('#status')).toHaveText('記事Aを保存しました', { timeout: 1000 });
});
first() pressed the "Save" button in the top row, which is Article B, and the assertion afterward failed (lines after Timeout are omitted):
[ '記事B 保存', '記事A 保存' ]
✘ 1 tests/first.spec.ts:7:1 › 並び順が変わっても first() で記事Aを押せるか (1.2s)
✓ 2 tests/first.spec.ts:14:1 › 並び順が変わっても filter なら記事Aを押せる (138ms)
1) tests/first.spec.ts:7:1 › 並び順が変わっても first() で記事Aを押せるか ─────────────────────────────────────────
Error: expect(locator).toHaveText(expected) failed
Locator: locator('#status')
Expected: "記事Aを保存しました"
Received: "記事Bを保存しました"
This test failed on the assertion after the click, so the mistake was caught. A test that only clicks and never checks the result would pass while pressing the wrong row. The same list with a filter() on the Article A text passed and pressed Article A regardless of the order.
When multiple matches are expected, check the count or the list
The strict rule applies only to actions that target a single element. The official documentation explains that operations for multiple elements, such as count(), work without a problem. To check that there is one "Save" button per row, use toHaveCount() or pass an array to toHaveText():
await expect(page.getByRole('button', { name: '保存', exact: true })).toHaveCount(2);
await expect(page.getByRole('listitem').getByRole('button')).toHaveText(['保存', '保存']);
Both assertions passed without an error, even though the locators match two elements.
When hidden elements are being counted
Sometimes a locator matches two elements even though only one is visible on screen. I tested a page that has both a desktop menu and a mobile menu in the HTML, with the mobile menu hidden by CSS:
// tests/hidden.spec.ts
const html = `
<nav class="pc"><button>ログイン</button></nav>
<nav class="sp" style="display: none"><button>ログイン</button></nav>
`;
test('locator で押す', async ({ page }) => {
await page.setContent(html);
await page.locator('button', { hasText: 'ログイン' }).click();
});
page.locator('button', ...) counts the button hidden with display: none as well, so it matched two elements and raised the error:
Error: locator.click: Error: strict mode violation: locator('button').filter({ hasText: 'ログイン' }) resolved to 2 elements:
1) <button>ログイン</button> aka getByRole('button', { name: 'ログイン' })
2) <button>ログイン</button> aka getByText('ログイン').nth(1)
On the same page, a getByRole('button', ...) locator for the login button passed. As the description of the includeHidden option says, getByRole() excludes hidden elements from matching by default.
If you want to keep a CSS selector locator, you can limit it to visible elements with visible(), which was added in Playwright 1.63:
const buttons = page.locator('button', { hasText: 'ログイン' });
console.log('count:', await buttons.count());
console.log('visible count:', await buttons.visible().count());
await buttons.visible().click();
count: 2
visible count: 1
Only one visible button remained, and click() passed. In versions before 1.63, the official documentation says the same narrowing is possible with the visible option of filter(), available since 1.51.
Look at the elements listed in the error and find what makes the one you want different from the others (which row it is in, whether its name is an exact match, whether it is visible). Add that condition to the locator, and it will match one element.





