tomcam
tomcam

Reputation: 5285

Chromium/chrome running headless: wait for page load when using --screenshot?

Trying to take a screenshot using headless Chrome but it executes --screenshot before the page is loaded. On MacOS running zsh, I'm invoking it like this:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome https://example.com/foo.html --screenshot=/Users/myhome/foo.png

Is there a way to wait until the page is loaded before the screenshot is taken?

Upvotes: 3

Views: 3598

Answers (1)

tomcam
tomcam

Reputation: 5285

The answer, it turns out, is that those command line options don't work well and have been supplanted by Puppeteer. Here's the script I created (hardcodes values for brevity).

Here's the complete code.

const puppeteer = require('puppeteer');
const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}
async function run () {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://twitter.com/gnuman1979/status/1239523796542992387');
  await sleep(2000)
  await page.screenshot({path: './foo.png'});
  browser.close();
}
run();

Save as foo.js, then run using node:

$ node foo.js

Upvotes: 4

Related Questions