Created
August 21, 2017 14:28
-
-
Save aryehof/1b180075caf19e158fa812010d8baf1a to your computer and use it in GitHub Desktop.
Puppeteer example screenshoting the logo on chromestatus.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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