Skip to content

Instantly share code, notes, and snippets.

@shkaper
Created November 21, 2018 12:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shkaper/5421a9135f26ab57d368ae8bb39dcd22 to your computer and use it in GitHub Desktop.
Save shkaper/5421a9135f26ab57d368ae8bb39dcd22 to your computer and use it in GitHub Desktop.
puppeteer screenshot all DOM nodes
const puppeteer = require('puppeteer')
;(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://youtube.com');
// get a list of all elements - same as document.querySelectorAll('*')
const elements = await page.$$('*')
let successful = 0
let notVisible = 0
let other = 0
console.log('node count:', elements.length)
for (let i = 0; i < elements.length; i++) {
try {
console.log(`node ${('0000' + i).slice(-5)}`)
// get screenshot of a particular element
await elements[i].screenshot({path: `shots/${('0000' + i).slice(-5)}.png`})
successful++
} catch (e) {
// if element is 'not visible', spit out error and continue
if (!e.message.includes('Node is either not visible or not an HTMLElement')) {
console.log(`couldnt take screenshot of element with index: ${i}. cause: `, e.message)
other++
} else {
notVisible++
}
}
}
console.log('successful:', successful)
console.log('notVisible:', notVisible)
console.log('other:', other)
await browser.close();
console.log('done')
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment