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

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Use Puppeteer’s ElementHandle.screenshot() to capture one DOM element. Query the element, confirm it exists, wait for the page content you need, then save the result to a file or use the returned image bytes. Puppeteer scrolls the element into view when needed; if it is detached from the DOM before capture, the method throws an error.

Capture one element with Puppeteer

Here is a complete JavaScript example using Puppeteer’s documented API. It opens a page, waits for a target element, captures it as a PNG, and disposes of the element handle when finished.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  try {
    const page = await browser.newPage();
    await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });

    const element = await page.waitForSelector('#target');
    if (!element) {
      throw new Error('Target element not found');
    }

    try {
      await element.screenshot({ path: 'element.png' });
    } finally {
      await element.dispose();
    }
  } finally {
    await browser.close();
  }
})();

Replace the URL and #target with the page and selector you need. page.waitForSelector() waits for a matching element; alternatively, page.$() queries once and returns a handle or null. Check the result before calling screenshot(). Puppeteer’s ElementHandle reference documents both handle creation and disposal.

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

TypeScript

In TypeScript, the handle can be typed to the expected DOM element, such as HTMLDivElement, to improve type checking. The capture flow is otherwise the same:

const element = await page.waitForSelector<HTMLDivElement>('#target');
if (!element) throw new Error('Target element not found');
try {
  await element.screenshot({ path: 'element.png' });
} finally {
  await element.dispose();
}

Make the page ready before capturing

The element method scrolls the target into view if necessary, then uses the page screenshot mechanism. That does not guarantee the application’s data, images, fonts, or animations have finished rendering. Wait for a condition that matters to your page before capturing: for example, a result selector, a loading indicator to disappear, or an application-specific ready state.

  1. Navigate: use page.goto() with a wait condition appropriate to the site. domcontentloaded means the initial document has been parsed; it is not a guarantee that every image or application request is complete.
  2. Wait for the target: use waitForSelector() when it may appear asynchronously, or query after your own readiness condition.
  3. Wait for important content: use the selector or state your application exposes to indicate that data or assets relevant to the shot are ready.
  4. Capture promptly: if the application may rerender or remove the node, reacquire the handle immediately before the screenshot.

For API details on page navigation and methods, see the Puppeteer Page class reference. Readiness conditions should reflect the application; a generic wait cannot establish that every site is visually settled.

Choose the output format and destination

ElementHandle.screenshot() accepts screenshot options. With no encoding option, its returned value is a Uint8Array. Set path to save directly to disk; if omitted, the image is not written to a file. A relative path is resolved from the current working directory.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Option What it does Useful detail
path Saves the image to a file. The extension is used to infer the screenshot type. A relative path is relative to the current working directory.
type Selects the image format. PNG is the documented default; JPEG and WebP are also listed formats.
quality Sets lossy image quality. Valid range is 0–100; it does not apply to PNG.
omitBackground Omits the default white background. Use it when transparency is needed; the documented default is false.
clip Specifies a screenshot rectangle. For a single DOM element, prefer the element handle method unless a specific rectangle is needed.
captureBeyondViewport Controls capture beyond the viewport in conjunction with clipping. The documented default is false when no clip is provided and true otherwise.
fullPage Captures the full page. The documented default is false. This is a page-scope option, not a substitute for selecting one element.

For lossless output or transparency, PNG is a sensible choice. JPEG or WebP can be appropriate when smaller lossy images suit the receiving workflow; check the resulting format and visual quality there. These are format-selection considerations, not claims of measured file-size or speed differences. See the complete ScreenshotOptions reference.

Save to disk or use the bytes in memory

To save a file, pass { path: 'element.png' }, as in the main example. To process the screenshot in memory, omit path and use the returned bytes:

const imageBytes = await element.screenshot({ type: 'png' });
// imageBytes is a Uint8Array; pass it to your image-processing or storage code.

If a base64 string is more suitable for your consumer, request it explicitly:

const base64Image = await element.screenshot({ encoding: 'base64' });

Choose the output form based on what the next step needs: a file path for a local artifact, bytes for programmatic processing, or base64 for interfaces that require text-encoded image data.

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.

Element screenshot or page screenshot?

Use the capture method whose scope matches the result you need. An element screenshot targets one DOM element; a page screenshot captures the page viewport or, with the appropriate option, the full page.

Need Use Why
One card, chart, panel, or other DOM node ElementHandle.screenshot() It captures the rendered element and scrolls it into view if necessary.
The visible page viewport Page.screenshot() It is the page-level screenshot method.
The full document rather than one node Page.screenshot({ fullPage: true }) The page screenshot options include full-page capture.
A precise rectangular area Page.screenshot() with a clip, where appropriate A clip defines a screenshot rectangle; it is not a DOM selector.

Consult Puppeteer’s Page.screenshot() method reference for page-scope behavior. The documentation also notes that, within a BrowserContext, creating or closing a page waits for a screenshot to finish, while Page.bringToFront() does not wait for existing screenshot operations.

Handle lifecycle and detachment

An ElementHandle refers to a particular DOM node. Puppeteer documents that a handle keeps its element from being garbage-collected until the handle is disposed; navigation of the associated frame or destruction of its parent context auto-disposes it. Dispose of handles you no longer need, especially in longer-running processes.

If the site replaces or removes the node between selection and capture, the handle may be detached. Puppeteer documents that a detached element causes ElementHandle.screenshot() to throw. It does not promise an automatic retry. Reacquire the element after the relevant render state, then decide whether a retry is appropriate for your application.

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

Troubleshooting element screenshots

  • “Target element not found” or a null handle: the selector may be wrong, the content may not have rendered yet, or the element may be inside a frame. Verify the selector against the loaded page and wait for the application’s relevant state before querying.
  • Screenshot throws because the element detached: a rerender or navigation removed the node. Query it again close to capture time and handle the documented error; do not assume Puppeteer will retry automatically.
  • The element is missing from the image: confirm that the selected node is the intended element and that it is present when capture begins. The method scrolls an element into view if needed, but it does not establish that dynamic content is ready.
  • Content appears incomplete: wait for application data and visual assets that matter to the capture. A navigation event alone may not match the page’s own completion condition.
  • No file appears: ensure you passed a path, check that the destination is writable, and remember that a relative path uses the process’s current working directory.
  • The output format is unexpected: check the file extension when relying on inference, or set type explicitly. Use a supported format and do not expect quality to change PNG output.
  • The background is opaque: request omitBackground: true if transparency is appropriate, and use an output format and downstream workflow that preserve it.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Performance, reliability, and cost considerations

Puppeteer’s element screenshot API provides capture behavior, not a guarantee about the performance or reliability of the page being captured. For repeatable output, reduce avoidable waits by waiting on meaningful application conditions rather than arbitrary long delays; avoid retaining handles longer than necessary; and capture only the scope and format your workflow needs. Image quality, page complexity, network activity, and the chosen readiness condition can all affect your overall process, but the cited API references do not establish benchmark figures or a fixed capture cost.

For batch jobs, handle failures per URL or item so one detached element or failed navigation does not silently invalidate the rest of the run. Log the URL, selector, and error category needed to diagnose a failure, while avoiding sensitive page data in logs. Retry only when your application can safely reacquire the target and a transient rerender is a plausible cause.

Or skip the browser setup

If you need screenshots through an API rather than managing a Puppeteer browser, ScreenshotNeo offers a one-request capture. Its clean-shot steps can accept cookie or consent banners and remove more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. It also provides an MCP server with screenshot, page-info, and PDF tools for AI agents.

cURL example, with the required target URL adapted to this guide’s example:

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://example.com -o shot.webp

See the ScreenshotNeo documentation for request details. The Free plan includes 1,000 shots per month without a card; paid plans start at $5 for 3,000 shots. Sign up for ScreenshotNeo’s free plan.

Frequently Asked Questions

Can Puppeteer return an element screenshot without writing a file?

Yes. Omit the path option; the default result is a Uint8Array. Set encoding: 'base64' when a base64 string is the required output.

Does ElementHandle.screenshot() capture the whole page?

No. It captures the selected element. Use Page.screenshot() for a viewport or full-page capture.

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.

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.