puppeteer - 无头 Chrome 节点 API 和 Puppeteer 安装
问题描述
在干净的 ubuntu 18.04 上安装 chrome headless 的整个过程中,我遇到了很多问题。github 上的设置指南对于干净的 ubuntu 18.04 来说是不够的
以下是设置无头 chrome 替代 phantomjs 的一些错误和答案/解决方案。
错误 1
(node:23835) UnhandledPromiseRejectionWarning: Error: Chromium revision is not downloaded. Run "npm install" or "yarn install"
at Launcher.launch owlcommand.com /puppeteer/node_modules/puppeteer/lib/Launcher.js:112:15)
at <anonymous>
(node:23835) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:23835) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
错误 2
(node:25272) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
owlcommand.com /puppeteer/node_modules/puppeteer/.local-chromium/linux-594312/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory
解决方案
基于https://github.com/GoogleChrome/puppeteer
您只需在 Ubuntu 18.04 中运行以下命令
npm i puppeteer
不幸的是,这还不够。
您将需要以下依赖项
sudo apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
之后,如果您按照他们的示例运行它,您将收到一个错误
(node:28469) UnhandledPromiseRejectionWarning: Error: Failed to launch chrome!
[1025/150325.817887:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
解决方案是
const browser = await puppeteer.launch({args: ['--no-sandbox']});
添加 --no-sandbox
它将相应地工作。完整的工作源代码如下
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();
await page.goto('http://owlcommand.com');
await page.screenshot({path: 'example.png'});
await browser.close();
})();
puppeteer@1.9.0~install的解决方法:cannot run in wd %s %s (wd=%s)
npm install --unsafe-perm
屏幕截图大小
默认真的很小,如果你正在测试的页面是响应式的,你可以用不同的视口设置来测试它。您可以通过 setViewport 方法更改其尺寸。
await page.setViewport({
width: 1600,
height: 1000
});
最新 Puppeteer 更新(2020 年 8 月)
sudo apt-get install libgbm1(必需)
推荐阅读
- java - SpringMVC 无法正确绑定 json
- amazon-web-services - 如何将 aws 凭证密钥传递给 KinesisProducerConfiguration 以在 Java 中创建 kinesis 生产者
- javascript - 当表单具有数据类型为“文本”的复选框和锚标记时如何添加验证
- amazon-web-services - Cloudwatch 用于基于使用 cloudwatch 指标的每日计费警报
- java - Java 验证方法并根据输入参数返回消息
- node.js - 谷歌云函数用40+函数写配额问题
- python - 如何高效地使用 Google Colab?
- node.js - 获取时获取json值作为key
- visual-studio - Xamarin.Forms Android 项目在 VS2019 中丢失了所有引用
- javascript - 如何删除d3中分组条形图中条形之间的空格