Getting Started with Locators
Locators are the recommended way to find and interact with elements in Puppeteer Sharp. They provide built-in auto-retry and auto-wait functionality, making your automation scripts more reliable.
What are Locators?
Section titled “What are Locators?”A Locator represents a way to find element(s) on a page. Every time an action is performed on a locator, the element is re-located, which helps avoid stale element references. Before performing actions, locators automatically check preconditions like visibility, enabled state, and stable positioning.
Creating Locators
Section titled “Creating Locators”CSS Selector Locator
Section titled “CSS Selector Locator”Use Page.Locator() with a CSS selector to create a locator:
var locator = page.Locator("button.submit");await locator.ClickAsync();Function Locator
Section titled “Function Locator”Use Page.LocatorFunction() to create a locator that evaluates a JavaScript function:
var locator = page.LocatorFunction("() => document.querySelector('button')");await locator.ClickAsync();Frame Locators
Section titled “Frame Locators”Locators also work with frames:
var frame = page.MainFrame.ChildFrames[0];await frame.Locator("button.submit").ClickAsync();Configuration
Section titled “Configuration”Locators use a fluent API for configuration. All setter methods return the locator instance for chaining.
SetTimeout
Section titled “SetTimeout”Sets the timeout in milliseconds. Default is 30000 (30 seconds).
await page.Locator("button.submit") .SetTimeout(5000) .ClickAsync();SetVisibility
Section titled “SetVisibility”Sets whether to wait for the element to be visible or hidden.
// Wait for element to be visible before clickingawait page.Locator("button.submit") .SetVisibility(VisibilityOption.Visible) .ClickAsync();
// Wait for a loading spinner to disappearawait page.Locator(".spinner") .SetVisibility(VisibilityOption.Hidden) .WaitAsync();SetWaitForEnabled
Section titled “SetWaitForEnabled”Sets whether to wait for the element to be enabled before acting. Default is true.
await page.Locator("button.submit") .SetWaitForEnabled(true) .ClickAsync();SetEnsureElementIsInTheViewport
Section titled “SetEnsureElementIsInTheViewport”Sets whether to automatically scroll the element into the viewport. Default is true.
await page.Locator("button.submit") .SetEnsureElementIsInTheViewport(true) .ClickAsync();SetWaitForStableBoundingBox
Section titled “SetWaitForStableBoundingBox”Sets whether to wait for the element’s bounding box to stabilize (stop moving) before acting. Default is true.
await page.Locator("button.submit") .SetWaitForStableBoundingBox(true) .ClickAsync();Combining Options
Section titled “Combining Options”await page.Locator("button.submit") .SetTimeout(10000) .SetVisibility(VisibilityOption.Visible) .SetWaitForEnabled(true) .SetEnsureElementIsInTheViewport(true) .SetWaitForStableBoundingBox(true) .ClickAsync();Actions
Section titled “Actions”ClickAsync
Section titled “ClickAsync”Clicks the located element.
await page.Locator("button.submit").ClickAsync();See Locator Actions for click options (button, count, delay, offset).
HoverAsync
Section titled “HoverAsync”Hovers over the located element.
await page.Locator("button.submit").HoverAsync();FillAsync
Section titled “FillAsync”Fills the located element with a value. Works with <input>, <textarea>, <select>, and contenteditable elements.
// Fill a text inputawait page.Locator("input[name='email']").FillAsync("user@example.com");
// Select an optionawait page.Locator("select#country").FillAsync("US");ScrollAsync
Section titled “ScrollAsync”Scrolls the located element.
await page.Locator("div.scrollable").ScrollAsync(new LocatorScrollOptions{ ScrollTop = 500});See Locator Actions for scroll options.
Waiting
Section titled “Waiting”WaitAsync
Section titled “WaitAsync”Waits for the locator to locate an element matching the configured preconditions.
// Wait for an element to appearawait page.Locator("div.loaded").WaitAsync();WaitAsync<T>
Section titled “WaitAsync<T>”Waits for the locator to get a value from the page, returned as a JSON-deserialized object.
var value = await page.LocatorFunction("() => document.title") .WaitAsync<string>();WaitHandleAsync
Section titled “WaitHandleAsync”Waits for the locator to get a handle from the page.
var handle = await page.Locator("button.submit").WaitHandleAsync();Composition
Section titled “Composition”Filter
Section titled “Filter”Creates a new locator that filters elements using a JavaScript predicate. If the predicate returns false, the locator retries.
await page.Locator("button") .Filter("element => element.textContent === 'Submit'") .ClickAsync();Creates a new locator that maps the located element using a JavaScript function.
var text = await page.Locator("div.content") .Map("element => element.textContent") .WaitAsync<string>();Locator.Race
Section titled “Locator.Race”Races multiple locators and resolves with the first one to find an element.
await Locator.Race( page.Locator("button.accept"), page.Locator("button.dismiss")).ClickAsync();CancellationToken Support
Section titled “CancellationToken Support”All locator actions accept a CancellationToken through their options parameter:
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await page.Locator("button.submit").ClickAsync(new LocatorClickOptions{ CancellationToken = cts.Token});await page.Locator("div.loaded").WaitAsync(new LocatorActionOptions{ CancellationToken = cts.Token});Made with love by Dario Kondratiuk