allwright

API ReferenceTypeScriptLocator

Locator

Every action and query method on Page (click, fill, hover, press, focus, highlight, count, textContent, innerText, waitFor) is also available directly on a Locator, scoped to whatever it resolves to. What's below is what a Locator adds: building one semantically instead of by raw selector, and narrowing it once built.

getByRole

MethodSince v0.1.3

Finds elements by ARIA role, with optional name/exact/checked/disabled/expanded/level/pressed/selected/includeHidden filters.

getByRole(role: string, options?: RoleOptions): Locator

Example

page.getByRole('button', { name: 'Buy', exact: true })

getByText / getByLabel / getByPlaceholder / getByAltText / getByTitle

MethodSince v0.1.3

Finds elements by visible text, associated label, placeholder, alt text, or title attribute. TextMatcher is string | RegExp.

getByText(text: TextMatcher, options?: TextOptions): Locator

Example

page.getByLabel('Email', { exact: true }).fill('you@example.com')

getByTestId

MethodSince v0.1.3

Finds elements by data-testid attribute.

getByTestId(text: TextMatcher): Locator

Locator.filter

MethodSince v0.1.3

Narrows a locator by a required or forbidden descendant (has/hasNot, same page only), by text (hasText/hasNotText), or by visible.

filter(options?: LocatorFilterOptions): Locator

Example

page.getByRole('listitem').filter({ has: page.getByRole('heading', { name: /^Beta$/ }) })

Locator.not

MethodSince v0.1.3

Excludes elements also matched by other (both locators must share the same page).

not(other: Locator): Locator

Example

page.getByRole('button').not(page.getByRole('button', { disabled: true }))

Locator.nth / first / last

MethodSince v0.1.3

Selects one match by position — nth accepts negative indices; first/last are sugar for nth(0)/nth(-1).

nth(index: number): Locator
first(): Locator
last(): Locator

Locator.locator

MethodSince v0.0.33

Chains a nested/relative locator off this one.

locator(selector: string, options?: LocatorFilterOptions): Locator