Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11In Playwright, locate a button by its role and accessible name, click it, then assert the result:
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page.getByText('Welcome, John!')).toBeVisible();
This pattern targets a button as a user encounters it and verifies that the interaction worked. Playwright’s current documentation recommends locators such as getByRole(); locator actions wait for the element to be ready, but a click still fails if the target is ambiguous, disabled, obscured, or otherwise not actionable.
Write a click as a user-facing interaction
A Playwright click starts with a locator: an instruction for finding the element when the action runs. For a button with the accessible name “Sign in,” use:
await page.getByRole('button', { name: 'Sign in' }).click();
getByRole('button') identifies the control by its role, while { name: 'Sign in' } narrows it by its accessible name. This is generally a better default than a selector tied to the page’s HTML layout: it describes the control’s meaning and is understandable to people maintaining the test. Playwright’s locator guide explains role and other built-in locators.
#1 Best Overall
A click only sends the action. To test the behavior, follow it with an assertion about what the user should see next:
import { test, expect } from '@playwright/test';
test('signs in', async ({ page }) => {
await page.goto('https://example.com');
await page.getByRole('button', { name: 'Sign in' }).click();
await expect(page.getByText('Welcome, John!')).toBeVisible();
});
Replace the example URL, button name, and expected confirmation with the application’s actual values. An assertion makes the test check an outcome rather than merely that code issued a click.
Choose a locator that identifies the intended button
Use the most meaningful locator your page supports. A locator must match exactly one element for a click; if more than one button matches, Playwright reports a strictness error rather than silently picking one.
Role and accessible name: the default
await page.getByRole('button', { name: 'Save changes' }).click();
await page.getByRole('button', { name: 'Save changes', exact: true }).click();
The first form can match a name containing the supplied text. Add exact: true when the distinction matters—for example, where “Save” and “Save changes” both appear. Accessible names may come from visible text or accessible labeling, so use the name the control actually exposes, not necessarily a guess based on its appearance.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Scan for outdated or missing drivers - takes under a minuteDriver Scan →For intentional name variants, a regular expression can be useful:
await page.getByRole('button', { name: /save/i }).click();
Use a broad pattern only when it still identifies one button. If it matches several, narrow it or scope the search to a relevant part of the page.
Scope the locator when buttons repeat
When a page has several buttons named “Delete,” first locate the relevant row or region, then find the button inside it. The exact parent locator depends on your markup; for example:
const row = page.getByRole('row', { name: /monthly report/i });
await row.getByRole('button', { name: 'Delete' }).click();
This says which record the action applies to rather than choosing whichever “Delete” button happens to come first. If a role-based parent is unavailable, a stable container locator can serve the same purpose.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitchesRank #3
Text, test IDs, and CSS
- Text:
getByText()is useful when visible wording is the clearest identifier, but confirm it selects the button itself and not a nearby label or incidental text. - Test ID:
getByTestId()is appropriate when the application deliberately exposes a stable testing identifier or user-facing attributes are unsuitable. Playwright lets a project configure which attribute it treats as a test ID; see its best-practices guidance. - CSS or XPath: use these when a semantic locator does not fit the case, but avoid long selectors coupled to layout, generated classes, or incidental DOM structure. Such details can change without changing what the button does.
Positional methods such as first(), last(), and nth() can address repeated matches, but they do not explain which item is intended. A page change can make the same position refer to a different button. Prefer a locator that identifies the correct control by context whenever possible.
What Playwright checks before clicking
Playwright automatically waits for the target to be actionable before performing a locator click. The documented checks include that the locator matches exactly one element and that the element is visible, stable, enabled, and able to receive events. “Stable” means it is not moving or animating; receiving events means another element, such as an overlay, is not intercepting the click. The details are in Playwright’s auto-waiting and actionability documentation.
If the checks do not pass before the applicable timeout, the click fails with a timeout error. This wait is useful for ordinary rendering delays: there is usually no need to insert a fixed sleep before clicking. But waiting cannot make an actually disabled button enabled, uncover a button blocked by a modal, or choose between two matching controls. Those require fixing the locator or arranging the page into the intended state.
Use click options only when the interaction needs them
A normal button interaction generally needs no options. The Locator API documents options for mouse button, click count, delay, keyboard modifiers, click position, timeout, forcing a click, and a trial action. Consult the Locator API reference for the complete current signatures and defaults.
The Tool Desk
Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Double-clicks, modifiers, and position
When the application specifically responds to a double-click, choose a click count explicitly:
await page.getByRole('button', { name: 'Open editor' }).dblclick();
For a chorded interaction, provide the required modifier, such as Shift:
await page.getByRole('button', { name: 'Select range' }).click({ modifiers: ['Shift'] });
A position option can target a particular point within an element, such as when the control’s behavior depends on where it is clicked. Avoid using coordinates merely to bypass a locator problem: coordinates are more sensitive to layout changes than a semantic locator.
Trial checks and forced clicks
To check whether actionability conditions pass without actually clicking, use trial: true:
await page.getByRole('button', { name: 'Continue' }).click({ trial: true });
A forced click bypasses actionability checks, including the check that the target receives click events. It can make a test proceed despite an overlay or other obstacle, but that can conceal a broken user interaction. Use force: true only when bypassing those checks is intentional—not as a routine fix for a timeout.
Assert the effect, including navigation
Choose an assertion that represents the behavior the test is meant to protect: a confirmation, changed control state, dialog, or destination content. Playwright’s assertions retry while waiting for their condition, rather than checking only once immediately after the click. For example:
await page.getByRole('button', { name: 'Save changes' }).click();
await expect(page.getByText('Changes saved')).toBeVisible();
If the click initiates navigation, a locator click waits for that navigation to succeed or fail by default. Assert something meaningful on the destination page as well; successful navigation by itself may not prove the expected content loaded.
Troubleshoot a click that fails
Start with the error and the current page state. A timeout usually means Playwright could not satisfy an actionability check in time; a strictness error means the locator matched more than one target.
| Symptom | Likely cause | What to do |
|---|---|---|
| Strictness or multiple-match error | The locator identifies more than one element. | Use an exact accessible name, scope to the relevant dialog or row, or choose a more specific locator. Do not default to first() unless position itself is the intended rule. |
| Click times out on visibility or enabled state | The button is hidden, disabled, or the page has not reached the expected state. | Check the actual rendered state and the workflow that should enable or reveal it. Wait for a meaningful condition when necessary; do not assume a fixed delay will correct a state problem. |
| Click times out because the target is unstable | The button is still moving or animating. | Let the transition finish or synchronize on the stable state that matters to the test. |
| Click times out because the target does not receive events | An overlay, pop-up, or another element covers the target. | Determine whether the overlay should be dismissed or the page state is wrong, then interact as a user would. Do not force the click just to hide the interception. |
| Locator finds no intended button | The accessible name, role, or assumed page state does not match the rendered page. | Inspect the actual control and its accessible name, then update the locator to match the application’s behavior. |
| Click passes, but the test fails afterward | The expected outcome did not occur, or the assertion describes the wrong result. | Check what the application did after input and assert the intended confirmation, state change, dialog, or destination content. |
When diagnosing a failure, inspect the match count and the visible page state before changing the click. Ask whether the locator is unique, whether the button is enabled and visible, and whether anything is intercepting input. A more precise locator or a correctly prepared page state is usually a better fix than turning off checks.
Or skip the browser setup
If your goal is to capture a page screenshot rather than exercise a button in a Playwright test, ScreenshotNeo offers a one-request screenshot API. It is not a replacement for testing button behavior: use Playwright when you need to interact with the page and verify what happens.
Quick Recap
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
See the ScreenshotNeo API documentation for request options. ScreenshotNeo removes cookie banners, popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed; an MCP server lets AI agents take screenshots; and 1,000 screenshots a month are free with no card, with paid plans starting at $5 for 3,000. Learn about ScreenshotNeo or sign up free.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

