October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Add a Script to a Frame in Puppeteer

Use frame.addScriptTag() to inject a script into a specific Puppeteer iframe. Learn how to identify the right frame, choose content, URL, or path, and troubleshoot navigation and path issues.

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.

To add a script to an iframe in Puppeteer, find the intended Frame and call await frame.addScriptTag(...). page.addScriptTag() is a shortcut for the page’s main frame; it does not target an arbitrary child frame.

Inject a script into the intended frame

Here is a runnable example for a page that contains a frame whose URL includes /embedded/:

As an Amazon Associate I earn from qualifying purchases.

const frame = page.frames().find(frame => frame.url().includes('/embedded/'));

if (!frame) {
  throw new Error('Target frame was not found');
}

await frame.addScriptTag({
  content: 'window.exampleFlag = true;',
});

Use a condition that uniquely identifies the frame on the page you are automating. Matching a distinctive URL fragment is one option; if the page exposes a stable frame name, you can inspect it through the frame’s element. A broad test such as “first child frame” can select the wrong frame when a page has multiple embeds.

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.

Choose how to provide the script

Frame.addScriptTag() accepts inline content, a hosted script URL, or a local file path. It returns a promise for a handle to the inserted script element.

Use case Example Notes
Inline JavaScript await frame.addScriptTag({ content: 'window.exampleFlag = true;' }); Useful for short snippets that should be added as a script element.
Hosted script await frame.addScriptTag({ url: 'https://example.test/script.js' }); The frame loads the script from the specified URL.
Local file await frame.addScriptTag({ path: './script.js' }); Relative paths are resolved from Node.js process.cwd(), not automatically from the JavaScript file’s directory.

The documented options are content, id, path, type, and url. Set type: 'module' when the script should load as an ES module. See the FrameAddScriptTagOptions reference for the option details.

Make sure you have the right frame

A Puppeteer page has a frame tree. Use page.mainFrame() for the top-level document and Frame.childFrames() to inspect a frame’s direct children. The page.frames() example above searches the frames currently known to the page. A Frame also exposes url() and frameElement(); Puppeteer’s Frame class reference demonstrates inspecting a frame element’s name.

Frames can attach, navigate, or detach as the page runs. If a page creates the iframe dynamically, wait until it is available before selecting it. If it navigates or is replaced, locate the current frame again rather than assuming a previously selected reference still represents the intended document. The Page class reference documents the page’s frame-related context.

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

Use the main-frame shortcut only for the top-level page

await page.addScriptTag(options) is documented as a shortcut for await page.mainFrame().addScriptTag(options). Use it when the script belongs in the top-level document. For an iframe, call addScriptTag() on that iframe’s Frame instead. See Page.addScriptTag().

Use frame-scoped evaluation when no script element is needed

If your goal is to run code in the frame rather than add a <script> element, use Frame.evaluate():

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

The function runs in the selected frame’s context, much like Page.evaluate() runs in the page context. Work in one frame does not automatically execute in its child frames, so select the frame where the code belongs. See the Frame.evaluate() reference.

Troubleshoot common problems

  • The target frame was not found: The iframe may not have attached yet, or the URL condition may not match its current URL. Wait for the page’s frame to become available, then inspect page.frames() and refine the identifying condition.
  • The script appears in the wrong document: Check whether you called page.addScriptTag(), which targets the main frame. Select the child Frame and call frame.addScriptTag().
  • The local script file cannot be found: Resolve the path relative to the process working directory. Check process.cwd() or provide the intended absolute path.
  • The frame navigated or disappeared: Pages can attach, navigate, and detach frames dynamically. Reacquire the intended frame after the change and inject into the current document.
  • The script runs in the parent but not an embedded document: Frame execution is scoped. Select and target each frame that needs the script; parent-frame evaluation does not automatically reach child frames.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If you only need a screenshot of a webpage rather than custom Puppeteer frame logic, ScreenshotNeo provides a website screenshot API. One GET request can return an image or PDF; for example, this cURL call saves a WebP screenshot of Stripe:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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 details. Cookie banners, popups, and chat widgets are removed before capture; bot checks, blank pages, failed loads, and cache hits are not billed. Its MCP server provides screenshot tools for AI agents, including Claude, Cursor, and other MCP clients. The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for 1,000 free screenshots a month, with no card required.

Best Value
The SQL Programming Language: .
  • Used Book in Good Condition

Frequently Asked Questions

Can I add a script to every frame with one call?

No. Select each frame that needs the script and call its addScriptTag() method.

Does Frame.addScriptTag() return the script element?

It returns a promise for a handle to the inserted script element.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.