Skip to main content
Locator represents a view to elements that match a selector. Locators are strict by default and automatically wait for elements to be actionable.

Constructor

Frame
required
Parent frame
string
required
Selector to locate elements
LocatorOptions
Locator options
  • hasText (string | RegExp): Matches elements containing text
  • hasNotText (string | RegExp): Matches elements not containing text
  • has (Locator): Matches elements containing another locator
  • hasNot (Locator): Matches elements not containing another locator
  • visible (boolean): Matches only visible elements

Filtering Methods

filter

Creates a filtered locator.
LocatorOptions
Filter options
Returns: Locator - Filtered locator

first

Returns first matching element.
Returns: Locator - Locator for first element

last

Returns last matching element.
Returns: Locator - Locator for last element

nth

Returns nth matching element.
number
required
Element index (0-based, negative for reverse)
Returns: Locator - Locator for nth element

and

Combines with another locator (AND).
Locator
required
Locator to combine with
Returns: Locator - Combined locator

or

Combines with another locator (OR).
Locator
required
Locator to combine with
Returns: Locator - Combined locator

Chaining Methods

locator

Creates a descendant locator.
string | Locator
required
Descendant selector or locator
LocatorOptions
Locator options
Returns: Locator - Descendant locator

getByRole

Finds by ARIA role.

getByText

Finds by text content.

getByLabel

Finds by label text.

getByPlaceholder

Finds by placeholder text.

getByAltText

Finds by alt text.

getByTitle

Finds by title attribute.

getByTestId

Finds by test ID.

frameLocator

Creates a frame locator.

contentFrame

Gets frame content locator.
Returns: FrameLocator - Frame locator

Action Methods

click

Clicks the element.
ClickOptions
Click options
  • button (‘left’ | ‘right’ | ‘middle’): Mouse button
  • clickCount (number): Number of clicks
  • delay (number): Delay between mousedown and mouseup
  • position (object): Click position
  • modifiers (string[]): Modifier keys
  • force (boolean): Force click
  • noWaitAfter (boolean): Don’t wait after click
  • timeout (number): Maximum time
Returns: Promise<void>

dblclick

Double clicks the element.

fill

Fills an input element.
string
required
Value to fill
FillOptions
Fill options
  • force (boolean): Force fill
  • noWaitAfter (boolean): Don’t wait after fill
  • timeout (number): Maximum time
Returns: Promise<void>

clear

Clears an input element.
FillOptions
Clear options
Returns: Promise<void>

type

Types text into element.
string
required
Text to type
TypeOptions
Type options
  • delay (number): Delay between key presses
  • timeout (number): Maximum time
Returns: Promise<void>

pressSequentially

Presses keys sequentially.

press

Presses a key.
string
required
Key to press (e.g., ‘Enter’, ‘ArrowDown’)
PressOptions
Press options
Returns: Promise<void>

check

Checks a checkbox.

uncheck

Unchecks a checkbox.

setChecked

Sets checkbox state.
boolean
required
Whether to check or uncheck
CheckOptions
Check options
Returns: Promise<void>

selectOption

Selects options.
string | string[] | SelectOption | SelectOption[]
required
Values to select
SelectOptionOptions
Select options
Returns: Promise<string[]> - Selected values

hover

Hovers over element.

tap

Taps the element.

focus

Focuses the element.

blur

Blurs the element.

dragTo

Drags to another locator.
Locator
required
Target locator
DragAndDropOptions
Drag options
Returns: Promise<void>

setInputFiles

Sets input files.
string | string[] | FilePayload | FilePayload[]
required
Files to upload
SetInputFilesOptions
Upload options
Returns: Promise<void>

dispatchEvent

Dispatches a DOM event.
string
required
Event type
any
Event initialization object
TimeoutOptions
Timeout options
Returns: Promise<void>

Content Methods

textContent

Returns text content.
Returns: Promise<string | null> - Text content or null

innerText

Returns inner text.
Returns: Promise<string> - Inner text

innerHTML

Returns inner HTML.
Returns: Promise<string> - Inner HTML

inputValue

Returns input value.
Returns: Promise<string> - Input value

getAttribute

Returns attribute value.
string
required
Attribute name
TimeoutOptions
Timeout options
Returns: Promise<string | null> - Attribute value or null

allTextContents

Returns all text contents.
Returns: Promise<string[]> - Array of text contents

allInnerTexts

Returns all inner texts.
Returns: Promise<string[]> - Array of inner texts

State Methods

isVisible

Checks if visible.
Returns: Promise<boolean> - True if visible

isHidden

Checks if hidden.
Returns: Promise<boolean> - True if hidden

isEnabled

Checks if enabled.
Returns: Promise<boolean> - True if enabled

isDisabled

Checks if disabled.
Returns: Promise<boolean> - True if disabled

isChecked

Checks if checked.
Returns: Promise<boolean> - True if checked

isEditable

Checks if editable.
Returns: Promise<boolean> - True if editable

Evaluation Methods

evaluate

Evaluates JavaScript on element.
Function
required
Function to evaluate
any
Argument to pass
TimeoutOptions
Timeout options
Returns: Promise<R> - Evaluation result

evaluateAll

Evaluates on all matching elements.

evaluateHandle

Evaluates and returns JSHandle.

Utility Methods

count

Returns number of matching elements.
Returns: Promise<number> - Count of elements

all

Returns all matching locators.
Returns: Promise<Locator[]> - Array of locators

boundingBox

Returns bounding box.
Returns: Promise<Rect | null> - Bounding box or null

screenshot

Takes a screenshot.
ScreenshotOptions
Screenshot options
  • path (string): File path to save to
  • type (‘png’ | ‘jpeg’): Image type
  • quality (number): JPEG quality
  • omitBackground (boolean): Transparent background
  • timeout (number): Maximum time
  • mask (Locator[]): Elements to mask
Returns: Promise<Buffer> - Screenshot buffer

scrollIntoViewIfNeeded

Scrolls element into view.
Returns: Promise<void>

selectText

Selects text.
Returns: Promise<void>

waitFor

Waits for element state.
WaitForOptions
Wait options
  • state (‘attached’ | ‘visible’ | ‘hidden’ | ‘detached’): Element state
  • timeout (number): Maximum time
Returns: Promise<void>

elementHandle

Returns element handle.
Returns: Promise<ElementHandle> - Element handle

elementHandles

Returns all element handles.
Returns: Promise<ElementHandle[]> - Array of element handles

page

Returns parent page.
Returns: Page - Parent page

highlight

Highlights the element.
Returns: Promise<void>

ariaSnapshot

Returns ARIA snapshot.
Returns: Promise<string> - ARIA snapshot

describe

Adds custom description.
string
required
Custom description
Returns: Locator - Locator with description

description

Returns custom description.
Returns: string | null - Custom description or null

Usage Examples

Basic Locator Usage

Filtering Locators

Using Get By Methods

Chaining Locators

Working with Multiple Elements