|-转 PHP采集时报错Failed to launch the browser process puppeteer
Chrome headless doesn't launch on UNIX,谷歌无头浏览器不能在unix运行,要安装对应的依赖侯才能正常运行无头模式。
具体解决办法参看 https://wokan.chawen.org/post/1167
Chrome headless doesn't launch on Windows
Some chrome policies might enforce running Chrome/Chromium with certain extensions.
Puppeteer passes --disable-extensions flag by default and will fail to launch when such policies are active.
To work around this, try running without the flag:
const browser = await puppeteer.launch({
ignoreDefaultArgs: ['--disable-extensions'],
});
Context: issue 3681.
Chrome headless doesn't launch on UNIX
Make sure all the necessary dependencies are installed. You can run ldd chrome | grep not on a Linux machine to check which dependencies are missing. The common ones are provided below.
Debian (e.g. Ubuntu) Dependencies (Debian系统依赖)
ca-certificates fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 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 lsb-release wget xdg-utils
CentOS Dependencies (Centos系统依赖)
alsa-lib.x86_64 atk.x86_64 cups-libs.x86_64 gtk3.x86_64 ipa-gothic-fonts libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXrandr.x86_64 libXScrnSaver.x86_64 libXtst.x86_64 pango.x86_64 xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-fonts-cyrillic xorg-x11-fonts-misc xorg-x11-fonts-Type1 xorg-x11-utils
After installing dependencies you need to update nss library using this command
yum update nss -y
Check out discussions
Chrome headless disables GPU compositing
Chrome/Chromium requires --use-gl=egl to enable GPU acceleration in headless mode.
const browser = await puppeteer.launch({ headless: true, args: ['--use-gl=egl'], });
Chrome is downloaded but fails to launch on Node.js 14
If you get an error that looks like this when trying to launch Chromium:
(node:15505) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
spawn /Users/.../node_modules/puppeteer/.local-chromium/mac-756035/chrome-mac/Chromium.app/Contents/MacOS/Chromium ENOENT
This means that the browser was downloaded but failed to be extracted correctly. The most common cause is a bug in Node.js v14.0.0 which broke extract-zip, the module Puppeteer uses to extract browser downloads into the right place. The bug was fixed in Node.js v14.1.0, so please make sure you're running that version or higher. Alternatively, if you cannot upgrade, you could downgrade to Node.js v12, but we recommend upgrading when possible.
Setting Up Chrome Linux Sandbox
In order to protect the host environment from untrusted web content, Chrome uses multiple layers of sandboxing. For this to work properly, the host should be configured first. If there's no good sandbox for Chrome to use, it will crash with the error No usable sandbox!.
If you absolutely trust the content you open in Chrome, you can launch Chrome with the --no-sandbox argument:
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
NOTE: Running without a sandbox is strongly discouraged. Consider configuring a sandbox instead.
There are 2 ways to configure a sandbox in Chromium.
[recommended] Enable user namespace cloning
User namespace cloning is only supported by modern kernels. Unprivileged user namespaces are generally fine to enable, but in some cases they open up more kernel attack surface for (unsandboxed) non-root processes to elevate to kernel privileges.
sudo sysctl -w kernel.unprivileged_userns_clone=1
[alternative] Setup setuid sandbox
The setuid sandbox comes as a standalone executable and is located next to the Chromium that Puppeteer downloads. It is fine to re-use the same sandbox executable for different Chromium versions, so the following could be done only once per host environment:
# cd to the downloaded instance
cd /node_modules/puppeteer/.local-chromium/linux-/chrome-linux/
sudo chown root:root chrome_sandbox
sudo chmod 4755 chrome_sandbox
# copy sandbox executable to a shared location
sudo cp -p chrome_sandbox /usr/local/sbin/chrome-devel-sandbox
# export CHROME_DEVEL_SANDBOX env variable
export CHROME_DEVEL_SANDBOX=/usr/local/sbin/chrome-devel-sandbox
You might want to export the CHROME_DEVEL_SANDBOX env variable by default. In this case, add the following to the ~/.bashrc or .zshenv: ...