Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node puppeteer take screenshot full page SPA

I have a single page application with scrolling. I am trying to take a screenshot of the whole page, but it only gives me the visible part. How can I make it shoot the whole page?

  const browser = await puppeteer.launch(options);
  const page = await browser.newPage();
  await page.goto(url);
  await page.screenshot({ path: 'page.png', fullPage: true })
  await browser.close();
like image 652
JsFan Avatar asked Dec 03 '17 09:12

JsFan


People also ask

How do I take a screenshot of a whole puppeteer?

Create a Browser Instance and a New Page // Create a browser instance const browser = await puppeteer. launch(); // Create a new page const page = await browser. newPage(); We can take the screenshot in different device views by setting the viewport size.

How do I take a screenshot of a whole webpage?

Click the three-dot icon from the top-right corner and select Run command. Also, you can press Ctrl+Shift+P on Windows or Command+Shift+P on Mac. Type screenshot into the search box. Select Capture full-size screenshot.

How do I take a screenshot of the entire developer tools?

Open DevTools by going to View ➙ Developer ➙ Developer Tools (Option + ⌘ + I) Go to ellipses menu ➙ Run command (Shift + ⌘ + P) Type “screenshot” Select the type of screenshot your want to take: area, full size, node, or regular.


1 Answers

Actually what is happening here that your page might took a while to load in full. So we have to increase the timeout. And before taking screen shot take a short break of 500ms and then it will take full page screenshot. Try below code.

const puppeteer = require('puppeteer');

async function runTest() {
const browser = await puppeteer.launch({
    headless: false,
    timeout: 100000
});

const page = await browser.newPage();
const url = 'https://stackoverflow.com/questions/47616985/node-puppeteer-take-screenshot-full-page-spa';

await page.goto(url, {
    waitUntil: 'networkidle2'
});
await page.waitFor(500);

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

runTest();
like image 198
Hemant Sankhla Avatar answered Oct 05 '22 21:10

Hemant Sankhla