Skip to content

Instantly share code, notes, and snippets.

@benjaminadk
Last active June 22, 2020 04:58
Show Gist options
  • Save benjaminadk/812886cb8f7df4a65c5a87a909d0805f to your computer and use it in GitHub Desktop.
Save benjaminadk/812886cb8f7df4a65c5a87a909d0805f to your computer and use it in GitHub Desktop.
Simple Puppeteer Screenshot Application
const puppeteer = require("puppeteer")
const path = require("path")
const DIRECTORY = "https://ladesignconcepts.com/shop-by-brand/"
const main = async (device) => {
const browser = await puppeteer.launch()
try {
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
await page.goto(DIRECTORY)
const brands = await page.$$(".shop-list a")
for (let [i, brand] of brands.entries()) {
const href = await page.evaluate(el => el.href, brand)
const text = await page.evaluate(el => el.textContent, brand)
const newPage = await browser.newPage()
await newPage.setViewport({
width:
device === "mobile"
? 400
: device === "tablet-h"
? 1024
: device === "tablet-v"
? 768
: 1360,
height:
device === "mobile"
? 800
: device === "tablet-h"
? 768
: device === "tablet-v"
? 1024
: 768
})
await newPage.goto(href)
await newPage.screenshot({
path: path.join(__dirname, "screenshots", device, `${text}.png`)
})
console.log(`Screenshot saved for ${text}`)
await newPage.close()
}
await browser.close()
} catch (error) {
console.log(error)
} finally {
console.log(`Screenshots saved to ${path.join(__dirname, "screenshots")}`)
}
}
main('desktop')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment