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

Use await page.evaluate(pageFunction, ...args) to run JavaScript in the browser page and get its result back in Node.js. The callback runs in the page’s context, not your script’s Node.js scope: pass values it needs as arguments, and use evaluateHandle when you need to keep a DOM object by reference.

Run JavaScript in the page with page.evaluate

page.evaluate accepts a function (or a string), runs it in the page, and returns its result to your Puppeteer script. Puppeteer’s API recommends a function because it is easier to debug and works better with TypeScript. If the function returns a Promise, Puppeteer waits for it to resolve. See the Page.evaluate API and the JavaScript execution guide.

const title = await page.evaluate(() => document.title);
console.log(title);

The callback’s return value must be transferable back to Node.js as a serializable value. Strings, numbers, booleans, arrays, and ordinary serializable objects are suitable. A DOM node is not returned as a live Node.js DOM object; for example, returning document.body through evaluate does not give your script a usable browser element reference.

Pass Node.js data into the page function

The evaluated callback is serialized and run in the page context. It cannot close over variables or helper functions declared only in your Node.js script. Pass needed values after the callback; Puppeteer supplies them as positional arguments.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
const suffix = ' — inspected';
const label = await page.evaluate(
  value => document.title + value,
  suffix,
);
console.log(label);

Define browser-side logic inside the callback, or pass data explicitly. A JSHandle can also be passed as an argument when the page function needs an object already obtained from the page.

Wait for asynchronous browser-side work

Make the callback async when it needs to await browser-side work. Puppeteer waits for the returned Promise to resolve, but that does not automatically wait for every application-specific condition your page might have.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
const readyState = await page.evaluate(async () => {
  await new Promise(resolve => setTimeout(resolve, 100));
  return document.readyState;
});
console.log(readyState);

Await the outer page.evaluate call as well. If you need to wait until a particular element or application state exists, use an appropriate Puppeteer wait strategy rather than assuming a delay guarantees readiness.

Choose the right evaluation method

Need Method Behavior
Compute or read a serializable value from the page page.evaluate Runs a function in the page and returns its result, awaiting a returned Promise. Puppeteer API.
Keep a page object or DOM element by reference page.evaluateHandle Returns a JSHandle; an element reference is represented by an ElementHandle. Puppeteer API.
Run a callback on the first element matching a selector page.$eval Passes the matched element as the callback’s first argument and throws if there is no match. Puppeteer API.
Install setup before the page’s own scripts run page.evaluateOnNewDocument Runs after document creation but before page scripts, including on navigation and qualifying child-frame events. Puppeteer API.

Keep a DOM element with a handle

Use evaluateHandle when later operations need the actual in-page object rather than a serialized copy. The returned handle can be used in a subsequent evaluation; dispose of it when you are done so it no longer retains the page object.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
const body = await page.evaluateHandle(() => document.body);
try {
  const html = await body.evaluate(element => element.innerHTML);
  console.log(html);
} finally {
  await body.dispose();
}

A navigation or destruction of the execution context may dispose of handles automatically. The JSHandle API documents handle lifecycle behavior.

Evaluate a callback on one matching element

For a single selector match, $eval avoids writing a separate lookup inside the page callback:

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
const text = await page.$eval('h1', element => element.textContent);
console.log(text);

It throws if no element matches. If the element may appear later, wait for it with an appropriate Puppeteer wait or locator strategy before using $eval.

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

Run setup before site scripts

Use evaluateOnNewDocument when code must execute in a newly created document before that document’s scripts. It also applies during navigation and qualifying child-frame attachment or navigation events.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
await page.evaluateOnNewDocument(() => {
  // Runs in the new document before its scripts execute.
});

Troubleshoot common evaluation errors

  • A Node.js variable is undefined in the callback: the callback runs in the page context and cannot access Node.js lexical scope. Pass the value after the callback as an argument.
  • A returned DOM node is empty or unusable in Node.js: regular evaluation serializes the result rather than transferring a live DOM object. Use evaluateHandle for reference-based work.
  • The result is missing or still pending: await the page.evaluate call. If the callback returns a Promise, Puppeteer waits for its resolution before returning the result.
  • $eval throws: its selector matched no element at the time it ran. Wait for the relevant element or use a strategy suited to elements that appear asynchronously.
  • A long-lived page object remains retained: dispose of its handle when finished, unless navigation or context destruction has already disposed of it.
  • TypeScript accepts code that fails in the browser: Node-side types do not establish which globals exist in the browser runtime. Verify that the page context provides the APIs your callback uses.

Version note

Puppeteer’s documentation pages are rolling references. The documentation reviewed on October 3, 2026 identified the evaluate, $eval, and evaluateHandle API references as version 25.12.0, the JSHandle reference as 25.9.0, and evaluateOnNewDocument as 25.11.0; the JavaScript execution guide is labeled Next. Check the versioned API docs for behavior matching the Puppeteer version installed in your project.

Or skip the browser setup

If the goal is a screenshot rather than running arbitrary browser-side logic, ScreenshotNeo offers a one-request screenshot API. It is not a replacement for page.evaluate when your code needs to inspect or modify page state.

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. It removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed. An MCP server lets AI agents take screenshots, and the free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000. Sign up for the free plan.

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.

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