1

Current output I am trying to capture screenshot of webpage using puppeteer page.screenshot method. But when I see the output screenshot it misses some part of page which is shadow root element inside html. How can I capture shadow root element also with puppeteer screenshot method. I am currently using below code. Example URL is https://www.buybuybaby.com/

const browser = await puppeteer.launch();

const page = await browser.newPage();

await page.setExtraHTTPHeaders({
      'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
      accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
    })


await page.goto("https://www.buybuybaby.com/", {    //URL with shadow root element
            waitUntil: 'networkidle2',
            timeout: 160000   
})

await page.setViewport({
    width: 1600,
    height: 20000
  })

await page.screenshot({path: 'screenshot.png'});

await browser.close(); 

1 Answers1

0

Page seems to be lazyloading the links/products, try with this awesome answer

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({
        headless: false
    });
    const page = await browser.newPage();
    await page.goto('https://www.buybuybaby.com/', { waitUntil: 'load' });
    await page.setViewport({
        width: 1200,
        height: 800
    });

    const element = await page.$("#trendingProductsList"); 

    await autoScroll(page);

    await page.screenshot({
        path: 'yoursite.png',
        fullPage: true
    });

    await browser.close();
})();

async function autoScroll(page) {
    await page.evaluate(async () => {
        await new Promise((resolve, reject) => {
            var totalHeight = 0;
            var distance = 100;
            var timer = setInterval(() => {
                var scrollHeight = document.body.scrollHeight;
                window.scrollBy(0, distance);
                totalHeight += distance;

                if (totalHeight >= scrollHeight) {
                    clearInterval(timer);
                    resolve();
                }
            }, 100);
        });
    });
}

this is my screenshot

mapa0402
  • 454
  • 1
  • 8
  • 24