Last active
February 23, 2020 01:52
-
-
Save VesterDe/4daad7d758e74268f81293f1ab7a4201 to your computer and use it in GitHub Desktop.
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
const selectorShot = async (page, s, path) => { | |
const frame = await page.mainFrame(); | |
const el = await frame.$(s); | |
if (el) { | |
const box = await el.boundingBox(); | |
console.log(JSON.stringify(box)); | |
if (box) { | |
try { | |
await el.screenshot({ | |
path: path, | |
}); | |
} | |
catch (error){ | |
console.log(error) | |
} | |
} else { | |
console.log(`Element not visible for capturing: ${s}`); | |
} | |
} else { | |
console.log(`Element not found for capturing: ${s}`); | |
} | |
}; | |
const browser = await puppeteer.launch({ args: ["--no-sandbox", "--disable-setuid-sandbox"] }); | |
const page = await browser.newPage(); | |
await page.goto('https://www.nytimes.com'); | |
await selectorShot(page, '.branding', "TEST1.png"); | |
await selectorShot(page, '#site-index-navigation', "TEST2.png"); | |
await browser.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try this.