Playwright locators find page elements and retry until they appear
Answer: Locators are how Playwright finds elements on a page at any moment, and they are the center of its auto-waiting and retry behavior. Prefer user-facing locators such as getByRole, getByText, getByLabel, getByPlaceholder, getByAltText, getByTitle, and getByTestId.
Choosing a locator
getByRole is the recommended starting point because it uses explicit and implicit accessibility attributes. For a button labeled "Sign in", that is page.getByRole('button', { name: 'Sign in' }). getByTestId looks at a data-testid attribute by default, and that attribute name can be configured.
The class in source
The Locator constructor takes a frame, a selector string, and options. Options such as hasText, has, and hasNot are added onto _selector. A nested has or hasNot locator must belong to the same frame.
See also
Claims
- id: C1 claim: Locators are central to Playwright auto-waiting and retry-ability. source: S1 evidence: "are the central piece of Playwright's auto-waiting and retry-ability."
- id: C2 claim: Locators represent a way to find elements on the page at any moment. source: S1 evidence: "a way to find element(s) on the page at any moment."
- id: C3 claim: The Locator class is constructed from a frame, a selector, and options. source: S2 evidence: "constructor(frame: Frame, selector: string, options?: LocatorOptions) {"
- id: C4 claim: A nested has locator must belong to the same frame. source: S2 evidence: "Inner \"has\" locator must belong to the same frame."
Sources
- [S1] Locators. Playwright docs. https://playwright.dev/docs/locators . License: Apache-2.0. Accessed 2026-11-11.
- [S2] Locator class. Playwright source. https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/client/locator.ts . License: Apache-2.0. Accessed 2026-11-11.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: Playwright documentation and source (microsoft/playwright): Copyright (c) Microsoft Corporation. NOTICE: This software contains code derived from the Puppeteer project (https://github.com/puppeteer/puppeteer), available under the Apache 2.0 license (https://github.com/puppeteer/puppeteer/blob/master/LICENSE). Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Playwright docs and Playwright source (microsoft/playwright), Apache-2.0. Notices: Playwright documentation and source (microsoft/playwright): Copyright (c) Microsoft Corporation. NOTICE: This software contains code derived from the Puppeteer project (https://github.com/puppeteer/puppeteer), available under the Apache 2.0 license (https://github.com/puppeteer/puppeteer/blob/master/LICENSE). Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE).
Cite this
iinventedlight. 2026. "Playwright locators find page elements and retry until they appear." iinventedlight Library, iil:note:6kb542pr, version of 2026-11-11. https://iinventedlight.com/library/notes/playwright-locators/. License: CC-BY-4.0.
@misc{iil_note_6kb542pr,
author = {{iinventedlight}},
title = {{Playwright locators find page elements and retry until they appear}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-11},
url = {https://iinventedlight.com/library/notes/playwright-locators/},
note = {Permanent ID iil:note:6kb542pr. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/playwright-locators.md.