T
T
transcend2020-11-17 12:17:57
JavaScript
transcend, 2020-11-17 12:17:57

How to take screenshots of multiple sites with puppeteer?

Good afternoon, there is a puppeteer function to take a screenshot of a web page.

const puppeteer = require('puppeteer');
const siteURL = 'http://example.com'

async function getPic() {
  const browser = await puppeteer.launch(); 
  const page = await browser.newPage();
  await page.setViewport({width: 1600, height: 1200});
  await page.goto(siteURL,{ waitUntil: 'networkidle2' });
  await page.screenshot({path: '__PNG.png', fullPage: true});
  await browser.close();
}


How to make it so that you can pass an array of sites (for example, 5) to the function and the function sequentially takes a screenshot of each of the sites?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Inishev, 2016-08-26
@Sashjkeee

Somehow it's still possible.

$(window).on('scroll.toElement', function (e) {
    var s_top = $("body").scrollTop();
    var el = $("#popular").offset().top;
    if(s_top > el){
         console.log('123');
         $(window).off('scroll.toElement');
    }
});

A
Alexey P, 2020-11-17
@ruddy22

const puppeteer = require('puppeteer');
const siteURLs = ['http://example0.com', 'http://example1.com', 'http://example2.com'];

function makeScreenshotFromSite(url, page) {
   return page.setViewport({width: 1600, height: 1200})
     .then(() => {
       return page.goto(siteURL,{ waitUntil: 'networkidle2' });
     })
     .then(() => {
        return page.screenshot({path: '__PNG.png', fullPage: true});
     });
};

async function start(sites) {
  const browser = await puppeteer.launch(); 
  const page = await browser.newPage();
  const someRes = await Promise.all(
     sites.map(makeScreenshotFromSite)
  );
  await browser.close();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question