User Agents

Web browsers and everyday web tech, explained

Fix Chrome Screenshot Capture in Node.js

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through the most common causes of broken Chrome screenshots in Node.js and shows you how to fix them. You'll learn to correct headless mode flags, handle page load timing, manage viewport sizes, and deal with common errors like blank images or timeouts. By the end, you'll have a reliable script that captures full-page or element-specific screenshots using Puppeteer or Playwright.

What You'll Need

  • Node.js installed (v14 or later recommended)
  • A project directory with npm initialized
  • Puppeteer or Playwright installed (npm install puppeteer or npm install playwright)
  • Chrome or Chromium installed (Puppeteer downloads its own by default)

Step-by-Step

  1. Install the library. Run npm install puppeteer (or npm install playwright) in your project. If you prefer to use your system Chrome, set executablePath in the launch options.
  2. Launch the browser correctly. Use puppeteer.launch({ headless: 'new' }) for Puppeteer v19+ or chromium.launch({ headless: true }) for Playwright. If you're using an older version, headless: true works but may have quirks.
  3. Create a new page and set a realistic viewport. Before navigating, set the viewport size: await page.setViewport({ width: 1280, height: 800 }). This prevents screenshots from being too small or cut off.
  4. Navigate to the target URL. Use await page.goto(url, { waitUntil: 'networkidle0', timeout: 30000 }). This waits for network requests to finish, avoiding blank or partial captures.
  5. Wait for dynamic content. If the page uses JavaScript to render content, add await page.waitForSelector('your-selector') or a delay like await new Promise(r => setTimeout(r, 3000)).
  6. Capture the screenshot. For full page: await page.screenshot({ path: 'screenshot.png', fullPage: true }). For a specific element: await page.screenshot({ path: 'element.png', clip: await element.boundingBox() }).
  7. Close the browser. Always call await browser.close() in a finally block to free resources.
  8. Handle errors gracefully. Wrap your code in try/catch and log the error message. Common errors like TimeoutError or Page crashed can be retried with a fresh page.

Common Mistakes

  • Forgetting to set viewport. Default viewport is 800x600, which may cut off wider pages.
  • Using waitUntil: 'load' instead of 'networkidle0'. This can capture before images or fonts load.
  • Not waiting for fonts. Add await document.fonts.ready via page.evaluate to ensure text isn't missing.
  • Ignoring the fullPage option. Without it, you only get the viewport height, not the entire page.
  • Using an old headless mode. Puppeteer v19+ defaults to the new headless, but if you explicitly set headless: true on older versions, you may get blank screenshots on some sites.

Tips

  • Use page.setDefaultTimeout(30000) to avoid indefinite hangs.
  • For full-page screenshots, ensure the page has a fixed width (e.g., 1280px) to avoid horizontal scrollbars.
  • If the site blocks headless browsers, try adding a user agent: page.setUserAgent('Mozilla/5.0 ...').
  • Take a screenshot of a hidden element by first scrolling it into view: await element.scrollIntoViewIfNeeded().
  • Use type: 'jpeg' for smaller file sizes if quality is not critical.

Troubleshooting

Blank or white screenshot. This usually means the page didn't load fully. Increase the timeout, use waitUntil: 'networkidle0', and add a small delay. Also check that the URL is correct and accessible.

TimeoutError. The page is taking too long to load. Increase the timeout in goto or use waitUntil: 'domcontentloaded' if network idle is too strict.

Page crashed. This often happens with heavy pages. Try disabling hardware acceleration: add --disable-gpu to the launch arguments. Also ensure your system has enough memory.

Element not found. If waitForSelector fails, the element may be inside an iframe. Use page.frames() to locate the correct frame and then query within it.

Screenshot is cut off. If you're not using fullPage, the viewport height limits the capture. Set a taller viewport or use fullPage: true.

Chrome not found. If you're using Playwright, run npx playwright install to download browsers. For Puppeteer, it downloads Chromium automatically unless you set executablePath.

Frequently Asked Questions

Why is my screenshot blank when using Puppeteer?

Most likely the page hasn't finished loading or the content is rendered after the load event. Use waitUntil: 'networkidle0' and wait for a specific selector.

Can I take a screenshot of a specific element only?

Yes, use page.screenshot({ clip: await element.boundingBox() }) after locating the element with page.$(selector).

How do I capture a full-page screenshot in Playwright?

Use page.screenshot({ path: 'full.png', fullPage: true }). Make sure the page has a fixed width to avoid horizontal scroll.

What does headless: 'new' mean in Puppeteer?

It's the new headless mode that behaves more like the full Chrome browser, fixing many rendering issues. Use it if you're on Puppeteer v19 or newer.

Why does the screenshot have a white background but no text?

This often happens when fonts haven't loaded. Wait for document.fonts.ready before capturing.

Can I use system Chrome instead of the bundled Chromium?

Yes, set executablePath to the path of your Chrome executable in the launch options. This can help if the bundled version has issues.