I have a problem with a Puppeteer script I'm writing. I've gotten it to do 90% of what it needs to do, which is use the sitemap page for any given site and go to each page before taking a full-page screenshot. It also spits out a console.log of a massive array with all of the site URLs. What I need to do and can't get right is to pull the page URL for each page it opens during its for loop and use that as the name for the screenshot. So like if it goes to google.com, picture 1 is called "google.com/.png", then "google.com/dogpictures.png" etc.
I'm including it below, I'd love to hear any feedback:
const puppeteer = require("puppeteer");
(async () => {
'use strict'
const smta = require('sitemap-to-array')
const options = { returnOnComplete: true}
const sitemapUrl = 'https:____.org/page-sitemap.xml'
smta(sitemapUrl, options, (error, list) => {
if (error) {
console.error(error)
} else {
console.log(list)
useSitemapWithPuppeteer(list);
}
})
const useSitemapWithPuppeteer = async (array) => {
const urls = array.map(obj => obj.loc);
const browser = await puppeteer.launch({
headless: false,
defaultViewport: null,
});
for (let i = 0; i < urls.length; i++) {
const url = urls[i];
const page = await browser.newPage();
page.setDefaultNavigationTimeout(0);
await page.goto(`${url}`, { waitUntil: "networkidle2" });
const currentURL = page.url;
await page.screenshot({ path: "test" + i + ".png", fullPage: true });
}
await browser.close();
}})();