Puppeteer wait for all images to load then take screenshot

前端 未结 5 1088
抹茶落季
抹茶落季 2020-12-23 16:38

I am using Puppeteer to try to take a screenshot of a website after all images have loaded but can\'t get it to work.

Here is the code I\'ve got so far, I am using h

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-23 17:15

    Another option, actually evaluate to get callback when all images were loaded

    This option will also work with setContent that doesn't support the wait networkidle0 option

    await page.evaluate(async () => {
      const selectors = Array.from(document.querySelectorAll("img"));
      await Promise.all(selectors.map(img => {
        if (img.complete) return;
        return new Promise((resolve, reject) => {
          img.addEventListener('load', resolve);
          img.addEventListener('error', reject);
        });
      }));
    })
    

提交回复
热议问题