Skip to main content

Page

Learn about the Page object that creates locators

Overview

The Locator class provides precise element interaction capabilities. It resolves CSS or XPath selectors within a frame and performs low-level actions using Chrome DevTools Protocol (CDP). Create a locator through the page object:

Key Features

  • Lazy resolution - Selectors are resolved fresh on each action
  • Isolated execution - Runs in an isolated world, separate from page scripts
  • CDP-based - Uses Chrome DevTools Protocol for reliable interactions
  • Automatic cleanup - Releases remote objects automatically
  • Iframe support - Works seamlessly with iframes and shadow DOM

Interaction Methods

click()

Click the element at its visual center.
button
"left" | "right" | "middle"
Mouse button to use for the click.Default: "left"
clickCount
number
Number of consecutive clicks (for double-click, triple-click).Default: 1
The method:
  1. Scrolls element into view
  2. Gets element geometry
  3. Moves mouse to center
  4. Dispatches mousePressed and mouseReleased events

fill()

Fill an input, textarea, or contenteditable element.
value
string
required
The text value to fill into the element.
The method intelligently handles different input types:
  • Uses native value setter for special inputs (date, number, etc.)
  • Types text character-by-character for regular inputs
  • Clears existing content before filling

type()

Type text into the element with optional delay between keystrokes.
text
string
required
The text to type.
delay
number
Delay in milliseconds between each keystroke.If not specified, uses Input.insertText for efficiency.

hover()

Move the mouse cursor to the element’s center without clicking.
Scrolls the element into view and dispatches a mouse move event.

selectOption()

Select one or more options in a <select> element.
values
string | string[]
required
Option value(s) to select. For multi-select elements, pass an array.
Returns: Promise<string[]> - Array of values that were actually selected.

setInputFiles()

Set files on an <input type="file"> element.
files
string | string[] | FilePayload | FilePayload[]
required
File paths or file payloads to upload.File Path: Absolute or relative path to a fileFile Payload: Object with { name, mimeType, buffer }
FilePayload Interface:
Pass an empty array to clear the file selection.

State Methods

isVisible()

Check if the element is visible.
Returns: Promise<boolean> - true if element is attached and visible.

isChecked()

Check if a checkbox or radio button is checked.
Returns: Promise<boolean> - true if checked. Also considers aria-checked for ARIA widgets.

inputValue()

Get the current value of an input element.
Returns: Promise<string> - The element’s input value. Works with: <input>, <textarea>, <select>, contenteditable elements.

textContent()

Get the element’s text content (raw).
Returns: Promise<string> - The element’s textContent property.

innerText()

Get the element’s visible text (layout-aware).
Returns: Promise<string> - The element’s innerText property.

innerHtml()

Get the element’s HTML content.
Returns: Promise<string> - The element’s innerHtml.

Selection Methods

count()

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

nth()

Get a locator for the element at a specific index.
index
number
required
Zero-based index of the element to select.
Returns: Locator - New locator targeting the nth element.

first()

Get a locator for the first matching element.
Returns: Locator - Returns the same locator (querySelector already returns first match).

Utility Methods

highlight()

Visually highlight the element with an overlay.
durationMs
number
How long to display the highlight in milliseconds.Default: 800
borderColor
{ r, g, b, a? }
Border color RGBA values (0-255).Default: { r: 255, g: 0, b: 0, a: 0.9 } (red)
contentColor
{ r, g, b, a? }
Content fill color RGBA values (0-255).Default: { r: 255, g: 200, b: 0, a: 0.2 } (yellow)
Useful for debugging and visual verification.

scrollTo()

Scroll the element to a specific position.
percent
number | string
required
Scroll position as percentage (0-100).
For <html> or <body> elements, scrolls the window. Otherwise, scrolls the element itself.

centroid()

Get the center coordinates of the element.
Returns: Promise<{ x, y }> - Center point in CSS pixels.

backendNodeId()

Get the DOM backend node ID for the element.
Returns: Promise<BackendNodeId> - Unique identifier for the DOM node. Useful for identity comparisons without maintaining element handles.

sendClickEvent()

Dispatch a DOM click event directly on the element.
bubbles
boolean
Whether the event bubbles.Default: true
cancelable
boolean
Whether the event is cancelable.Default: true
composed
boolean
Whether the event crosses shadow DOM boundaries.Default: true
detail
number
Click count detail.Default: 1
This dispatches an event directly without synthesizing real pointer input. Useful for elements that rely on click handlers without needing hit-testing.

Code Examples

Selector Support

Locators support both CSS and XPath selectors:

CSS Selectors

XPath Selectors

Best Practices

  1. Use specific selectors - Prefer IDs or unique attributes over generic selectors
  2. Chain with nth() - Use locator().nth() instead of putting index in selector
  3. Check state before action - Use isVisible(), isChecked() for conditional logic
  4. Let locators auto-resolve - Don’t store element handles, use locators which re-resolve
  5. Use fill() for inputs - Prefer fill() over click() + type() for better reliability
  6. Handle file uploads properly - Use absolute paths or buffer payloads for setInputFiles()
  7. Highlight for debugging - Use highlight() during development to verify targeting

Common Patterns

Conditional Interaction

Wait and Interact

Loop Through Elements

Error Handling

Locator methods may throw the following errors:
  • Element not found - Selector doesn’t match any elements
  • Element not visible - Element exists but is not visible (for actions requiring visibility)
  • Invalid selector - Malformed CSS or XPath selector
  • Timeout errors - Operation exceeded timeout limits
  • CDP errors - Chrome DevTools Protocol communication errors
Handle errors appropriately:

Type Definitions