DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober 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 Now×
Skip to content
World desk4 min

How to Set Page Content in Puppeteer

Use Puppeteer’s page.setContent(html) to populate a page from an HTML string, then wait for the specific element or app state your next step needs.

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.

Use Puppeteer’s page.setContent(html, options?) method to replace a page’s document content with an HTML string. Await it before running the next step:

await page.setContent(`<!doctype html>
<html>
  <head><title>Example</title></head>
  <body><main><h1>Hello</h1></main></body>
</html>`);

setContent returns a promise that resolves to void. It accepts markup, not a URL. See the Puppeteer Page.setContent API reference.

As an Amazon Associate I earn from qualifying purchases.

Set HTML on a page

Call page.setContent(html) with the markup string, then await the promise before querying or interacting with the resulting page. A fragment is sufficient when you only need elements in the current page context; use a full document when you need document-level structure such as a title or explicit head and body.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
await page.setContent('<main><h1>Rendered markup</h1></main>');
const heading = await page.$eval('h1', element => element.textContent);
console.log(heading); // Rendered markup

The method sets supplied HTML as the page content; it does not navigate to a web address. For navigation to a URL, use a navigation method instead.

Choose a wait condition and timeout

The optional second argument follows Puppeteer’s wait-options contract. The documented default lifecycle condition is load, and the documented default timeout is 30,000 milliseconds. You can set them explicitly:

await page.setContent(html, {
  waitUntil: 'load',
  timeout: 30_000,
});

Use a lifecycle condition that fits the operation rather than increasing the timeout automatically. If waitUntil is an array of lifecycle events, Puppeteer waits for every named event in the array. The available options and defaults are described in the WaitForOptions reference.

You can also set a shared navigation timeout with page.setDefaultNavigationTimeout(milliseconds); Puppeteer documents that it applies to page.setContent as well. A per-call timeout lets you use a different limit for one call. See setDefaultNavigationTimeout.

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

Wait for the state your next step needs

Completion of setContent is not necessarily the same as an application-specific readiness condition. If later work depends on a particular element or state, wait for that explicitly after setting the markup.

Wait for an element

await page.setContent('<div id="app"></div>');
await page.waitForSelector('#app');

waitForSelector supports visibility and hidden-state conditions, a timeout, and an abort signal; see the waitForSelector reference.

Wait for an application condition

When readiness is represented by a JavaScript condition rather than the presence of an element, use waitForFunction:

await page.waitForFunction(() => window.appReady === true);

It waits until a function evaluated in the browser context returns a truthy value. Node.js arguments can also be passed to the function. See the waitForFunction reference. For interactions with elements, Puppeteer’s locator guide describes locators that wait for an element to be present and in the right state: Page interactions.

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

Set content inside a frame

Use frame.setContent when the target is a particular iframe or other frame, rather than the main page. Find and validate the frame before setting its markup:

Best Value
The SQL Programming Language: .
  • Used Book in Good Condition
const frame = page.frames().find(candidate => candidate.name() === 'preview');
if (!frame) throw new Error('Preview frame not found');
await frame.setContent('<p>Frame content</p>');

Frame.setContent accepts an HTML string and optional wait options, following the same basic pattern. See the Frame.setContent API reference.

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

Handle untrusted markup carefully

The API assigns the supplied string as page markup; the method reference does not promise to sanitize untrusted HTML or prevent scripts and external resources from running. Treat untrusted markup as browser content and apply the security controls appropriate to your application. Do not rely on setContent itself as a sanitizer.

Troubleshoot common problems

  • The following query runs too early: ensure the setContent call is awaited. Then add waitForSelector or waitForFunction if the next step relies on a specific element or application state.
  • The call times out: check which lifecycle condition is being awaited and whether it matches the task. Set a suitable per-call timeout or adjust the page’s default navigation timeout; a larger value alone does not establish that the application is ready.
  • The wrong content changes: confirm whether the target is the main page or an iframe. Use page.setContent for the page and the corresponding frame.setContent for a frame.
  • A URL appears where markup is expected: setContent accepts an HTML string, not a URL. Use a navigation method when the intent is to load a website.
  • The frame lookup fails: verify that the frame exists and that its name matches before calling setContent; frame names are only one possible way to identify the target.

Or skip the browser setup

If your goal is a website screenshot rather than running a Puppeteer browser yourself, ScreenshotNeo offers a one-request capture API. Cookie banners, newsletter popups and chat widgets are removed before capture; bot checks, blank pages and failed loads are never billed. An MCP server lets AI agents take screenshots, and 1,000 screenshots a month are free with no card; paid plans start at $5 for 3,000. See the ScreenshotNeo website and API documentation.

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

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

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

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.