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 DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

Wait for a Selector Before Taking a Puppeteer Screenshot

Wait for a Puppeteer selector before capture, choose page or element scope, set visibility and timeout options, and troubleshoot common failures.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use await page.waitForSelector(selector) before calling Puppeteer’s screenshot method. Add { visible: true } when the target must be visible, then call page.screenshot() for the page or ElementHandle.screenshot() for just the matched element.

Wait for the selector, then capture

This example waits until .target is present and visible, captures that element, and disposes of its handle:

As an Amazon Associate I earn from qualifying purchases.

const element = await page.waitForSelector('.target', { visible: true });
if (!element) {
  throw new Error('Target element was not found');
}
try {
  await element.screenshot({ path: 'target.png' });
} finally {
  await element.dispose();
}

waitForSelector() returns an ElementHandle for a match. The null check is defensive for code that may change its wait options; with the visible wait shown here, a timeout normally throws instead. The Puppeteer API reference documents the method and its options.

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

Choose presence or visibility

By default, waitForSelector() waits for the element to exist in the DOM. That does not necessarily mean a user can see it. Set { visible: true } to require both DOM presence and visibility. Conversely, { hidden: true } resolves when the element is hidden or absent; that wait can return null because there is no visible element handle to capture.

CSS selectors work directly. Puppeteer also supports selector syntax for text, accessibility role and name, XPath, and combinations that can cross shadow roots. See the selector and wait options reference for details.

Capture the whole page or one element

Capture the page after the target appears

Use the wait as a readiness condition, then call page.screenshot():

await page.waitForSelector('.target', { visible: true });
await page.screenshot({ path: 'page.png', fullPage: true });

fullPage: true captures the full page; it defaults to false. Use clip when you need a specific capture region. Puppeteer can infer the image format from the path extension. Options are described in its screenshots guide and ScreenshotOptions reference.

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.

Capture only the matched element

Call element.screenshot() on the handle returned by the wait. Puppeteer scrolls the element into view if needed. If the element is detached from the DOM before the screenshot, the capture throws. The ElementHandle screenshot reference covers this behavior.

Set a timeout and handle failures

The documented default timeout is 30 seconds. If the selector does not match before the timeout, Puppeteer throws rather than continuing to the screenshot. You can set a per-wait timeout, change the page default with Page.setDefaultTimeout(), or set timeout: 0 to disable the timeout. An AbortSignal can also cancel the wait. Prefer a finite timeout in automation so a missing target does not leave a capture hanging indefinitely.

const element = await page.waitForSelector('.target', {
  visible: true,
  timeout: 10_000,
});

The 10-second value here is an example choice, not Puppeteer’s default. Choose a limit that fits the site and your workflow.

Common failures and fixes

  • The wait times out: Check that the selector is correct and that the page has reached the state where the element is inserted. If visibility is not actually required, remove visible: true; otherwise, inspect why the target remains hidden.
  • The wait succeeds but the screenshot fails: The element may have been detached between the wait and capture. Re-query or retry the workflow around the page’s update, and avoid assuming a saved handle remains valid after the DOM changes.
  • The page shot is too short: Set fullPage: true if you need the full document rather than the current viewport.
  • The capture includes the wrong area: Use the returned handle’s screenshot for just the element, or configure clip for a page screenshot region.
  • Handles accumulate in a long-running script: Dispose of each returned ElementHandle when finished, such as in a finally block.

When to use a locator instead

Puppeteer’s current page-interactions guide recommends locators for selecting and interacting with elements because locators wait for action preconditions. waitForSelector() is a lower-level API and does not automatically retry an action. It remains a direct choice when you need the returned handle itself for ElementHandle.screenshot(). See the page interactions guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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 need a screenshot without managing a Puppeteer browser, ScreenshotNeo returns an image or PDF from one GET request. It can wait for a selector, and its clean-shot workflow accepts cookie or consent banners and removes supported consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, with page verdict and billing information in response headers. Its MCP server provides screenshot tools for AI agents.

Example cURL request, with the API key and target URL replaced as appropriate:

Best Value
The SQL Programming Language: .
  • Used Book in Good Condition
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 documentation for the API parameters, including selector waits. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Sign up for free.

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.

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.