Puppeteer wait for all images to load then take screenshot

前端 未结 5 1086
抹茶落季
抹茶落季 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:32

    I found a solution which is applicable to multiple sites using the page.setViewPort(...) method as given below:

    const puppeteer = require('puppeteer');
    
    async(() => {
        const browser = await puppeteer.launch({
            headless: true, // Set to false while development
            defaultViewport: null,
            args: [
                '--no-sandbox',
                '--start-maximized', // Start in maximized state
            ],
        });
    
        const page = await = browser.newPage();
        await page.goto('https://www.digg.com/', {
            waitUntil: 'networkidle0', timeout: 0
        });
    
        // Get scroll width and height of the rendered page and set viewport
        const bodyWidth = await page.evaluate(() => document.body.scrollWidth);
        const bodyHeight = await page.evaluate(() => document.body.scrollHeight);
        await page.setViewport({ width: bodyWidth, height: bodyHeight });
    
        await page.waitFor(1000);
        await page.screenshot({path: 'digg-example.png' });
    })();
    

提交回复
热议问题