elementHandle.screenshot() produces blank screenshots for elements outside of viewport
See original GitHub issueSteps 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:
- Created 5 years ago
- Reactions:15
- Comments:20
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I managed to fix my issue by setting the
viewport
before taking the screenshots. The important thing was to get the fullheight
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:To be clear, this code is now nicely working for us without (any yet discovered) issues: