Look up Playwright locator methods at a glance
Playwright’s Locator is a lazy, retrying handle to elements. This reference
groups the methods that create locators, filter and chain them, perform actions
and run web-first assertions, showing each signature and whether it returns a
chainable Locator. It runs entirely in your browser.
How it works
A locator is built with a creator like getByRole or locator, optionally
refined with filter or nth, then acted on or asserted. Creators and filters
return a new Locator (chainable); actions and expect() assertions are async
and must be awaited. Assertions auto-retry until the timeout:
const row = page.getByRole("row").filter({ hasText: "Acme Ltd" });
await row.getByRole("button", { name: "Edit" }).click();
await expect(page.getByText("Saved")).toBeVisible();
Tips and examples
- Prefer user-facing creators (
getByRole,getByLabel,getByText) over CSS; they survive refactors and double as accessibility coverage. - Use
filter({ has: locator })to keep only elements that contain a matching descendant — ideal for picking the right card or table row. nth(0),first()andlast()select among multiple matches; if a locator resolves to several elements an action will throw a strictness error.- Web-first assertions (
toBeVisible,toHaveText,toHaveCount) retry automatically, so you rarely need explicitwaitForcalls.