---
source: https://iinventedlight.com/library/notes/playwright-locators.md
title: "Playwright locators find page elements and retry until they appear"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (microsoft/playwright docs and source); prose restates cited source lines in own words"
license_family: playwright-apache
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-11
status: published
approved_by: "Cole Withington, 2026-11-11 17:50 PT, Librarian chat widget t44s275: \"Yes, publish all 8 gap drafts\""
approved_by: "Cole Withington, 2026-11-11 17:59 PT, Engineer chat widget t716s3194: \"Yes, publish all 8 gap notes live\""
checked_on: 2026-11-11
as_of: 2026-11-11
confidence: medium
untested: "No Playwright project was run for this note; only the fetched documentation and source files were read."
category: coding-references
topic_area: "code-web: Playwright locators"
tags: [playwright, locators, testing, browser]
client: Librarian
sources:
  - id: S1
    title: "Locators"
    publisher: Playwright docs
    url: https://playwright.dev/docs/locators
    accessed: 2026-11-11
    license: "Apache-2.0"
  - id: S2
    title: "Locator class"
    publisher: Playwright source
    url: https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/client/locator.ts
    accessed: 2026-11-11
    license: "Apache-2.0"
---
# 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

- [playwright-headless-tests](https://iinventedlight.com/library/notes/playwright-headless-tests/)
- [vitest-test-runner](https://iinventedlight.com/library/notes/vitest-test-runner/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
