Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call asLocator() on the handle: const locator = elementHandle.asLocator(); It is synchronous and returns a locator backed by that same DOM element—not a fresh selector lookup. If you need Puppeteer to resolve the element again when an action runs, use page.locator(selector) or frame.locator(selector) instead.

Convert an existing handle with asLocator()

Given an ElementHandle, call its asLocator() method directly. Do not use await on the conversion itself:

const locator = elementHandle.asLocator();

The documented signature is asLocator(this: ElementHandle<Element>): Locator<Element>. The method reference currently shown for Puppeteer 25.5.0 documents this signature; check the API reference and TypeScript definitions for the version installed in your project.

Example after waiting for a selector

const buttonHandle = await page.waitForSelector('button.submit');
if (!buttonHandle) {
  throw new Error('Submit button was not found');
}

const buttonLocator = buttonHandle.asLocator();
await buttonLocator.click();

The null check is appropriate for selector APIs that can return null. Confirm the return type and options for the Puppeteer version you use. The conversion is synchronous; the wait and click are asynchronous.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What the conversion does—and does not do

An ElementHandle refers to a particular DOM element. Calling asLocator() wraps that existing reference in Locator behavior; it does not preserve a selector and search the page again. Puppeteer’s API documentation notes that a handle-backed locator cannot refresh the handle if it becomes stale, though it can reuse locator preconditions.

Locators provide a way to perform actions with readiness checks. Puppeteer’s interaction guide describes waiting for the element to be present and relevant action conditions, including viewport presence, visibility, enabled state, and a stable bounding box for clicking. The guide is versioned 25.12.0, so check the documentation for your installed version for exact behavior.

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Choose handle-backed or selector-backed lookup

Approach Example Best fit Important limitation
Handle-backed locator handle.asLocator() You already have the intended element and want locator preconditions for actions. It remains tied to that handle and cannot refresh it if stale.
Selector-backed locator page.locator(selector) or frame.locator(selector) You want Puppeteer to locate the element from a selector when the action runs. You need a selector that identifies the intended element in the relevant page or frame.

Prefer a page or frame locator for a fresh lookup

await page.locator('button.submit').click();
// Or, inside a particular frame:
await frame.locator('button.submit').click();

Puppeteer recommends locators for selecting and interacting with elements. Use the handle conversion when you already obtained the specific element; use a page or frame locator when the action should resolve it from a selector.

Keep a lower-level API when you need it

Converting to a locator is not required just because Puppeteer offers locators. The interaction guide says lower-level APIs, including waitForSelector() and ElementHandle, remain available when they suit the operation. Use the API that matches whether you need an existing element reference, a selector-based lookup, or a lower-level operation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshooting

  • asLocator is not available: Check the Puppeteer version and the type of the value you are calling it on. The cited method reference documents it on ElementHandle<Element>; consult the API documentation and installed TypeScript definitions for your version.
  • The handle may be null: If the selector call can return null, check the result before calling asLocator(). Use the return type and options for your exact version to determine whether the check is needed.
  • The locator does not find a replacement after a DOM change: asLocator() is based on the original handle, not the selector that produced it. When you need a fresh resolution, create a locator from the selector with page.locator() or frame.locator().
  • An action does not proceed: Locators wait for action-related readiness conditions, but that does not make a stale handle refreshable. Confirm that the element is still the intended one and choose a selector-backed locator if fresh lookup is required.

Or skip the browser setup

If your goal is to capture a page rather than automate a particular DOM element, ScreenshotNeo offers a website screenshot API. One GET request returns a PNG, JPEG, WebP, or PDF. Cookie banners are accepted and known consent platforms, newsletter popups, and chat widgets are removed before capture; those steps can be disabled. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and the response identifies the page verdict and billing status. An MCP server lets AI agents use screenshot tools. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options, or visit ScreenshotNeo. Sign up free for 1,000 screenshots a month with no card.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Frequently Asked Questions

Does asLocator() re-query the element?

No. It creates a locator based on the existing ElementHandle; it does not perform a fresh selector lookup.

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Do I need to write await before asLocator()?

No. The conversion itself is synchronous. Await asynchronous operations such as waiting for a selector or clicking.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.