Overview
TheLocator 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.Mouse button to use for the click.Default:
"left"Number of consecutive clicks (for double-click, triple-click).Default:
1- Scrolls element into view
- Gets element geometry
- Moves mouse to center
- Dispatches mousePressed and mouseReleased events
fill()
Fill an input, textarea, or contenteditable element.The text value to fill into the element.
- 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.The text to type.
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.selectOption()
Select one or more options in a<select> element.
Option value(s) to select. For multi-select elements, pass an array.
Promise<string[]> - Array of values that were actually selected.
setInputFiles()
Set files on an<input type="file"> element.
File paths or file payloads to upload.File Path: Absolute or relative path to a fileFile Payload: Object with
{ name, mimeType, buffer }State Methods
isVisible()
Check if the element is visible.Promise<boolean> - true if element is attached and visible.
isChecked()
Check if a checkbox or radio button is checked.Promise<boolean> - true if checked. Also considers aria-checked for ARIA widgets.
inputValue()
Get the current value of an input element.Promise<string> - The element’s input value.
Works with: <input>, <textarea>, <select>, contenteditable elements.
textContent()
Get the element’s text content (raw).Promise<string> - The element’s textContent property.
innerText()
Get the element’s visible text (layout-aware).Promise<string> - The element’s innerText property.
innerHtml()
Get the element’s HTML content.Promise<string> - The element’s innerHtml.
Selection Methods
count()
Get the number of elements matching the selector.Promise<number> - Count of matching elements.
nth()
Get a locator for the element at a specific index.Zero-based index of the element to select.
Locator - New locator targeting the nth element.
first()
Get a locator for the first matching element.Locator - Returns the same locator (querySelector already returns first match).
Utility Methods
highlight()
Visually highlight the element with an overlay.How long to display the highlight in milliseconds.Default:
800Border color RGBA values (0-255).Default:
{ r: 255, g: 0, b: 0, a: 0.9 } (red)Content fill color RGBA values (0-255).Default:
{ r: 255, g: 200, b: 0, a: 0.2 } (yellow)scrollTo()
Scroll the element to a specific position.Scroll position as percentage (0-100).
<html> or <body> elements, scrolls the window. Otherwise, scrolls the element itself.
centroid()
Get the center coordinates of the element.Promise<{ x, y }> - Center point in CSS pixels.
backendNodeId()
Get the DOM backend node ID for the element.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.Whether the event bubbles.Default:
trueWhether the event is cancelable.Default:
trueWhether the event crosses shadow DOM boundaries.Default:
trueClick count detail.Default:
1Code Examples
- Basic Interaction
- Forms
- File Upload
- Element Selection
- State Checks
- Advanced Actions
Selector Support
Locators support both CSS and XPath selectors:CSS Selectors
XPath Selectors
Best Practices
- Use specific selectors - Prefer IDs or unique attributes over generic selectors
- Chain with nth() - Use
locator().nth()instead of putting index in selector - Check state before action - Use
isVisible(),isChecked()for conditional logic - Let locators auto-resolve - Don’t store element handles, use locators which re-resolve
- Use fill() for inputs - Prefer
fill()overclick()+type()for better reliability - Handle file uploads properly - Use absolute paths or buffer payloads for
setInputFiles() - 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

