Created
November 21, 2018 12:42
-
-
Save shkaper/5421a9135f26ab57d368ae8bb39dcd22 to your computer and use it in GitHub Desktop.
puppeteer screenshot all DOM nodes
This file contains hidden or 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
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