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

Run JavaScript in the frame you want to target with await frame.evaluate(pageFunction, ...args). The callback executes in that frame’s browser context; pass Node.js values as arguments, and Puppeteer awaits a returned promise before sending its result back.

Run JavaScript in a frame

First select the correct Puppeteer Frame from the page, then call its evaluate() method. This example finds a frame whose URL contains /widget and reads its document title:

const frame = page.frames().find(candidate => candidate.url().includes('/widget'));
if (!frame) throw new Error('Target frame was not found');

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

frame.evaluate() serializes the function and runs it in the selected frame, not in Node.js. Its result is returned to Node.js; if the function returns a promise, Puppeteer waits for it to resolve. See the Frame.evaluate() API reference and the Page.evaluate() API reference.

Select the frame you need

A page’s frame tree can change as embedded content attaches, navigates, or detaches. page.mainFrame() returns the main frame, while page.frames() returns the current frames. A frame also exposes childFrames() and parentFrame(). JavaScript evaluated in a parent frame does not automatically run in its nested child frames.

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.

Find a frame by URL

When a frame’s URL is distinctive, search the current frame list and handle the possibility that it is not present yet:

const frame = page.frames().find(candidate => candidate.url().includes('/checkout'));
if (!frame) throw new Error('Checkout frame not found');

Identify a frame by its iframe element

If URLs are ambiguous, inspect each frame’s associated iframe element and check its name or id. The current Frame API example uses frame.frameElement(); the reference marks frame.name() deprecated and recommends reading the iframe element instead.

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.
for (const candidate of page.frames()) {
  const frameElement = await candidate.frameElement();
  if (!frameElement) continue;

  const nameOrId = await frameElement.evaluate(el => el.name || el.id);
  if (nameOrId === 'payment-frame') {
    const result = await candidate.evaluate(() => document.body.innerText);
    console.log(result);
    break;
  }
}

The main frame may not have an iframe element, which is why the example skips candidates for which frameElement() returns no element. For dynamic pages, wait until the intended frame or content is available before evaluating. See the Frame class reference.

Pass values from Node.js into the frame

The callback passed to evaluate() is serialized. It cannot close over variables or helper functions in your Node.js scope. Pass any needed values as trailing arguments instead:

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 selector = '.status';
const status = await frame.evaluate(
  selector => document.querySelector(selector)?.textContent?.trim() ?? null,
  selector,
);

console.log(status);

In the browser callback, selector is the value supplied by Node.js. For multiple values, accept multiple callback parameters and pass those values after the function. Define helper logic inside the callback or pass the necessary data explicitly; a Node.js helper function is not available there just because it is in scope around the call.

Wait for dynamic content before evaluating

If the target element appears asynchronously, wait within the selected frame before querying it. frame.waitForSelector() waits for matching content in that frame and works across navigations. It returns an element handle; it throws if a required element does not appear, and the documented hidden case can return null.

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 frame = page.frames().find(candidate => candidate.url().includes('/checkout'));
if (!frame) throw new Error('Checkout frame not found');

await frame.waitForSelector('[data-ready="true"]');
const result = await frame.evaluate(() => ({
  title: document.title,
  ready: document.querySelector('[data-ready="true"]') !== null,
}));
console.log(result);

For interactions such as clicking or filling, Puppeteer locators are generally a better fit because they automatically wait for presence and state. Use custom evaluate() when you need browser-side JavaScript that the interaction API does not provide. The Frame.waitForSelector() reference and Page interactions guide describe these approaches.

Choose the right evaluation method

Method Use it for What comes back
frame.evaluate(fn, ...args) General-purpose JavaScript in a frame A serialized result; returned promises are awaited
frame.evaluateHandle(fn, ...args) A live reference to a DOM node or other browser object A handle that can be used for further browser-side work
frame.$eval(selector, fn, ...args) Run a function against the first matching element The function’s result
frame.$$eval(selector, fn, ...args) Run a function against matching elements The function’s result
frame.waitForSelector(selector, options) Wait for a selector to appear in the frame An element handle, or null in the documented hidden case
frame.locator(selector) Interactions such as clicking or filling A locator for actions with automatic waiting

Use ordinary evaluate() for strings, numbers, arrays, and plain objects you want to transfer to Node.js. Browser objects such as DOM nodes do not become usable live Node.js objects through ordinary serialization. Use evaluateHandle() when you need to retain a browser-side reference. For method details, see the JavaScript execution guide and the Frame.$eval() reference.

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.

Dispose of handles when finished

A handle is tied to its frame’s execution context and is disposed when that context is destroyed, such as when the frame navigates away. Dispose of handles you no longer need so they do not retain resources unnecessarily:

const bodyHandle = await frame.evaluateHandle(() => document.body);
try {
  const text = await bodyHandle.evaluate(body => body.innerText);
  console.log(text);
} finally {
  await bodyHandle.dispose();
}
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common frame-evaluation problems

  • The callback says a Node.js variable is undefined. The callback runs in the browser context and cannot access Node.js lexical variables. Pass the value as an argument, for example frame.evaluate(value => /* use value */, value).
  • The result is {} or does not act like a DOM node. Ordinary evaluate() serializes its result. Return serializable data for Node.js, or use evaluateHandle() to keep a browser-object reference.
  • A selector is missing. Confirm that you selected the correct frame, then wait with frame.waitForSelector(selector) or use a locator for an interaction. The wait can time out if the element never appears.
  • The script ran in the wrong document. Check the frame URL or inspect the associated iframe element’s name or id. A child frame’s DOM is not part of the parent frame’s document.
  • The element is inside a nested iframe. Find the nested frame in the frame tree and call evaluate() on that frame itself.
  • A frame disappears or changes during execution. Frame trees change during attachment, navigation, and detachment. Reacquire the intended frame after the change and wait for its target content before evaluating.
  • You no longer need a returned handle. Call its dispose() method when finished; navigation or context destruction also disposes handles associated with that context.

Or skip the browser setup

If your goal is to capture a page rather than run custom code inside its frame, ScreenshotNeo can return a screenshot or PDF through one GET request. Its cleanup accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. 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 exposes screenshot tools to AI agents, and the free plan includes 1,000 shots a month without a card; paid plans start at $5 for 3,000 shots. See the ScreenshotNeo website and API documentation.

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

Sign up for ScreenshotNeo’s free plan: 1,000 screenshots a month, no card required.

Version notes

The linked Puppeteer API references are labeled versions 25.10.0, 25.11.0, and 25.12.0, and the JavaScript execution guide is labeled “Next.” These sources describe the API behavior as documented on October 3, 2026; they do not establish a minimum Puppeteer version for the methods here. Check the API reference matching your installed version if you need version-specific signatures.

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.

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.