Skip to content

Instantly share code, notes, and snippets.

@aryehof
Created August 21, 2017 14:28
Show Gist options
  • Save aryehof/1b180075caf19e158fa812010d8baf1a to your computer and use it in GitHub Desktop.
Save aryehof/1b180075caf19e158fa812010d8baf1a to your computer and use it in GitHub Desktop.
Puppeteer example screenshoting the logo on chromestatus.com
// full example of screenshoting a DOM element
// using https://github.com/GoogleChrome/puppeteer
const PADDING = 16; // px
const SELECTOR = 'header aside'; // css selector for the element
// make sure we hit the desktop breakpoint
page.setViewport({width: 1000, height: 600, deviceScaleFactor: 2});
await page.goto('https://www.chromestatus.com/samples', {waitUntil: 'networkidle'});
const rect = await page.evaluate(selector => {
const element = document.querySelector(selector);
const {x, y, width, height} = element.getBoundingClientRect();
return {left: x, top: y, width, height, id: element.id};
}, SELECTOR);
await page.screenshot({
path: 'element.png',
clip: {
x: rect.left - PADDING,
y: rect.top - PADDING,
width: rect.width + PADDING * 2,
height: rect.height + PADDING * 2
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment