question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

elementHandle.screenshot() produces blank screenshots for elements outside of viewport

See original GitHub issue

Steps to reproduce

Tell us about your environment:

  • Puppeteer version: 1.3.0
  • Platform / OS version: OS X 10.12.6
  • URLs (if applicable): N/A (Test script uses https://github.com but I’ve noticed the issue on other pages)
  • Node.js version: 8.1.4

What steps will reproduce the problem? Using elementHandle.screenshot in the following test script, capturing an element outside of the viewport results in a blank / empty screenshot.

Please include code that reproduces the issue.


import puppeteer from 'puppeteer';

const VIEWPORT = {width: 1200, height: 900};
const URL = 'https://github.com/';
const TESTELEMENT = '.footer';

(async () => {
  const browser = await puppeteer.launch({headless: false});
  const page = await browser.newPage();
  await page.setViewport(VIEWPORT);
  await page.goto(URL, { waitUntil: 'networkidle0' });


  // Full page screenshot.
  await page.screenshot({
    path: 'tmp/testPage.png',
    fullPage: true
  });

  // Screenshot using element handle.
  const element = await page.$(TESTELEMENT);
  await page.waitFor(1000);
  await element.screenshot({
    path: 'tmp/testElement.png',
  });

  // Screenshot using the element's bounding box.
  const elementBounds = await element.boundingBox();
  await page.screenshot({
    path: 'tmp/testClip.png',
    clip: elementBounds,
  });

  await browser.close();
})()

What is the expected result?

The screenshot taken using the element handle should capture the footer for the url provided. The screenshot that supplies the element’s bounding box should also capture the footer.

What happens instead?

The full page screenshot is fine, but the other two are blank (white). If I adjust the script and increase the viewport height to something really large, like 9999px, the footer is within the initial viewport and the problematic screenshot come out fine.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:15
  • Comments:20

github_iconTop GitHub Comments

17reactions
marcelotedeschicommented, Feb 25, 2020

I managed to fix my issue by setting the viewport before taking the screenshots. The important thing was to get the full height of the page where everything I wanted to screenshot was visible. In my case, everything was a child of a .application-wrapper div. So the code looks something like this:

const VIEWPORT = { width: 1280, height: 720, deviceScaleFactor: 2 }; // Your default values
await page.goto(url, { waitUntil: 'networkidle0' });

const fullPage = await page.$('.application-wrapper');
const fullPageSize = await fullPage.boundingBox();
await page.setViewport(
  Object.assign({}, VIEWPORT, { height: fullPageSize.height })
);

// Element screenshot logic
const elements = await page.$$('.screenshot-element');

for (const [index, element] of elements.entries()) {
  await element.screenshot({ path: `/tmp/${index}.png` });
}
14reactions
bluepetercommented, May 16, 2019

To be clear, this code is now nicely working for us without (any yet discovered) issues:

        const screenshot = await chromePage.screenshot({
          clip: await targetElement.boundingBox(),
        });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Blank images in Puppeteer screenshots solved! - Peter Tran
... when using the elementHandle.screenshot function, capturing an element outside of the viewport results in a blank / empty screenshot.
Read more >
javascript - Puppeteer Not Waiting Until Page Load Complete
elementHandle.screenshot() scrolls to the element, but does not have a way to delay or wait for element to be visible.
Read more >
How to take website screenshots with Puppeteer - Urlbox
Let's try to capture a viewport screenshot of google.com. ... and then using the click() method on the first element handle in the...
Read more >
Puppeteer documentation - DevDocs
Defaults to an 800x600 viewport. null disables the default viewport. ... newPage() creates a page in the default browser context.
Read more >
puppeteer.ElementHandle.screenshot JavaScript and Node.js ...
This method scrolls element into view if needed, and then uses page.screenshot to take a screenshot of the element. If the element is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found