← All notes

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

Sources

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

Permanent ID: iil:note:6kb542pr

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.},
}

Markdown

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.