无头Chrome Node API和Puppeteer安装

Cod*_*uru 6 headless-browser google-chrome-headless puppeteer

在干净的ubuntu 18.04上无头安装chrome的整个过程中,我遇到了很多问题。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.
Run Code Online (Sandbox Code Playgroud)

错误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
Run Code Online (Sandbox Code Playgroud)

Cod*_*uru 18

基于https://github.com/GoogleChrome/puppeteer

您只需要在Ubuntu 18.04中运行以下命令

npm i puppeteer
Run Code Online (Sandbox Code Playgroud)

不幸的是,这还不够。

您将需要以下依赖关系

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
Run Code Online (Sandbox Code Playgroud)

之后,如果按照其示例运行它,将会收到错误消息

    (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.
Run Code Online (Sandbox Code Playgroud)

解决方案是

const browser = await puppeteer.launch({args: ['--no-sandbox']});
Run Code Online (Sandbox Code Playgroud)

添加--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();
})();
Run Code Online (Sandbox Code Playgroud)

puppeteer@1.9.0~install的解决方案:无法在wd%s%s中运行(wd =%s)

npm install --unsafe-perm
Run Code Online (Sandbox Code Playgroud)

屏幕截图大小

默认值很小,如果要测试的页面具有响应能力,则可以使用其他视口设置对其进行测试。您可以通过setViewport方法更改其尺寸。

await page.setViewport({
  width: 1600, 
  height: 1000
});
Run Code Online (Sandbox Code Playgroud)


Jua*_*uan 5

11 月 18 日更新:您不再需要 --no-sandbox 标志,您应该在发送到 .launch() 的对象中使用 headless:false 属性

const browser = await puppeteer.launch({
    headless: false,
    slowMo: 80,
    args: ['--window-size=1920,1080']
    });
Run Code Online (Sandbox Code Playgroud)

还要确保安装了所有必需的 debian 依赖项:

sudo apt-get install gconf-service libasound2 libatk1.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
Run Code Online (Sandbox Code Playgroud)