铬/铬无头运行:使用--screenshot时是否等待页面加载? [英] Chromium/chrome running headless: wait for page load when using --screenshot?

查看:51
本文介绍了铬/铬无头运行:使用--screenshot时是否等待页面加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用无头Chrome拍摄屏幕截图,但在加载页面之前会执行--screenshot.在运行zsh的MacOS上,我这样调用它:

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?

推荐答案

事实证明,答案是那些命令行选项无法正常运行,并且已被Puppeteer取代.这是我创建的脚本(为简洁起见,对值进行了硬编码).

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).

这是完整的代码.

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();

另存为foo.js,然后使用node运行:

Save as foo.js, then run using node:

$ node foo.js

这篇关于铬/铬无头运行:使用--screenshot时是否等待页面加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆