Skip to content

Instantly share code, notes, and snippets.

@crevulus
Last active October 27, 2023 15:06
Show Gist options
  • Save crevulus/28203ee6fb5fa85657dd4160ff2ea5a5 to your computer and use it in GitHub Desktop.
Save crevulus/28203ee6fb5fa85657dd4160ff2ea5a5 to your computer and use it in GitHub Desktop.
A small script using puppeteer to get screenshots of multiple browser pages.
const puppeteer = require("puppeteer");
const pages = require("./webPages.json");
async function takeMultipleScreenshots() {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
for (const { id, name, url } of pages) {
await page.goto(url);
await page.screenshot({ path: `screenshot${id}.jpeg`, fullPage: true });
console.log(`${name} - (${url})`);
}
} catch (err) {
console.log(`Error: ${err.message}`);
}
}
takeMultipleScreenshots();
[
{
"id": "c1472465-ede8-4376-853c-39274242aa69",
"url": "https://github.com/microsoft/vscode",
"name": "VSCode"
},
{
"id": "6b08743e-9454-4829-ab3a-91ad2ce9a6ac",
"url": "https://github.com/vuejs/vue",
"name": "vue"
},
{
"id": "08923d12-caf2-4d5e-ba41-3019a9afbf9b",
"url": "https://github.com/tailwindlabs/tailwindcss",
"name": "tailwindcss"
},
{
"id": "daeacf42-1ab9-4329-8f41-26e7951b69cc",
"url": "https://github.com/getify/You-Dont-Know-JS",
"name": "You Dont Know JS"
}
]
@Villegas-06
Copy link

I have a question, if I want browse one url more in array
({
"id": "6b08743e-9454-4829-ab3a-91ad2ce9a6ac",
"url2": "example.com",
"url": "https://github.com/vuejs/vue",
"name": "vue"
},).

¿How?

@crevulus
Copy link
Author

@Villegas-06 just add it after you await taking the other screenshots:

for (const { id, name, url, url2 } of pages) {
      await page.goto(url);
      await page.screenshot({ path: `screenshot${id}.jpeg`, fullPage: true });
      await page.goto(url2);
      await page.screenshot({ path: `screenshot${id}-2.jpeg`, fullPage: true });
      console.log(`${name} - (${url} & ${url2})`);
}

Bear in mind it's best to include a full URL with protocol, subdomain, etc.

@Villegas-06
Copy link

Ook, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment