故障排除
¥Troubleshooting
为了使此页面保持最新状态,我们很大程度上依赖社区的贡献。如果你发现某些内容不再是最新的,请发送 PR。
¥To keep this page up-to-date we largely rely on community contributions. Please send a PR if you notice something is no longer up-to-date.
Cannot find module 'puppeteer-core/internal/...'
如果你的 Node.js 版本低于 14 或者你使用自定义解析器(例如 jest-resolve
),则可能会发生这种情况。对于前者,我们不支持已弃用的 Node.js 版本。对于后者,通常升级解析器(或其父模块,例如 jest
)就可以了(例如 https://github.com/puppeteer/puppeteer/issues/9121)
¥This can occur if your Node.js version is lower than 14 or if you are using a
custom resolver (such as
jest-resolve
). For the former,
we do not support deprecated versions of Node.js. For the latter, usually
upgrading the resolver (or its parent module such as jest
) will work (e.g.
https://github.com/puppeteer/puppeteer/issues/9121)
Could not find expected browser locally
从 v19.0.0 开始,Puppeteer 将使用 os.homedir
将浏览器下载到 ~/.cache/puppeteer
,以便在 Puppeteer 升级之间实现更好的缓存。通常,主目录是明确定义的(即使在 Windows 上),但有时主目录可能不可用。在这种情况下,我们提供 PUPPETEER_CACHE_DIR
变量,允许你更改安装目录。
¥Starting from v19.0.0, Puppeteer will download browsers into
~/.cache/puppeteer
using
os.homedir
for better caching
between Puppeteer upgrades. Generally the home directory is well-defined (even
on Windows), but occasionally the home directory may not be available. In this
case, we provide the PUPPETEER_CACHE_DIR
variable which allows you to change
the installation directory.
例如,
¥For example,
- npm
- Yarn
- pnpm
PUPPETEER_CACHE_DIR=$(pwd) npm install puppeteer
PUPPETEER_CACHE_DIR=$(pwd) node <script-path>
PUPPETEER_CACHE_DIR=$(pwd) yarn add puppeteer
PUPPETEER_CACHE_DIR=$(pwd) node <script-path>
PUPPETEER_CACHE_DIR=$(pwd) pnpm add puppeteer
PUPPETEER_CACHE_DIR=$(pwd) node <script-path>
你还可以在应用的根目录下创建一个名为 .puppeteerrc.cjs
(或 puppeteer.config.cjs
)的配置文件,其中包含以下内容
¥You can also create a configuration file named .puppeteerrc.cjs
(or
puppeteer.config.cjs
) at the root of your application with the contents
const {join} = require('path');
/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};
你需要重新安装 puppeteer
才能使配置生效。请参阅 配置 Puppeteer 了解更多信息。
¥You will need to reinstall puppeteer
in order for the configuration to take
effect. See Configuring Puppeteer for more
information.
Chrome 无法在 Windows 上启动
¥Chrome doesn't launch on Windows
某些 chrome 政策 可能会强制运行带有某些扩展的 Chrome/Chromium。
¥Some chrome policies might enforce running Chrome/Chromium with certain extensions.
Puppeteer 默认情况下会传递 --disable-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'],
});
上下文:问题 3681。
¥Context: issue 3681.
Chrome 在 Windows 上报告沙箱错误
¥Chrome reports sandbox errors on Windows
Chrome 在 Windows 上使用沙箱,这需要对下载的 Chrome 文件具有额外的权限。从 Puppeteer v22.14.0 开始,Puppeteer 将尝试通过在浏览器安装期间运行 Chrome 提供的 setup.exe
工具来配置这些权限。
¥Chrome uses sandboxes on Windows which require additional permissions on
the downloaded Chrome files. Starting from Puppeteer v22.14.0, Puppeteer
will attempt to configure those permissions by running the setup.exe
tool provided by Chrome during the installation of the browser.
如果你使用的是旧版 Puppeteer 或仍然在浏览器输出中看到以下错误:
¥If you are using an older Puppeteer version or still seeing the following errors in the browser output:
[24452:59820:0508/113713.058:ERROR:sandbox_win.cc(913)] Sandbox cannot access executable. Check filesystem permissions are valid. See https://bit.ly/31yqMJR.: Access is denied. (0x5)
你可以使用 icacls 手动设置权限:
¥You can use icacls to set permissions manually:
icacls %USERPROFILE%/.cache/puppeteer/chrome /grant *S-1-15-2-1:(OI)(CI)(RX)
详细信息请参见 https://bit.ly/31yqMJR。
¥See https://bit.ly/31yqMJR for more details.
Chrome 无法在 Linux 上启动
¥Chrome doesn't launch on Linux
确保安装了所有必需的依赖。你可以在 Linux 计算机上运行 ldd chrome | grep not
以检查缺少哪些依赖。下面提供了常见的。另请参阅 https://source.chromium.org/chromium/chromium/src/+/main:chrome/installer/linux/debian/dist_package_versions.json 以获取 Chrome 安装程序声明的最新依赖列表。
¥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. Also, see
https://source.chromium.org/chromium/chromium/src/+/main:chrome/installer/linux/debian/dist_package_versions.json
for the up-to-date list of dependencies declared by the Chrome installer.
Chrome 目前不提供适用于 Linux 的 arm64 二进制文件。只有适用于 Mac ARM 的 arm64 二进制文件。这意味着默认下载的 Linux 二进制文件无法在 Linux arm64 上运行。
¥Chrome currently does not provide arm64 binaries for Linux. There are only arm64 binaries for Mac ARM. That means that Linux binaries downloaded by default will not work on Linux arm64.
Debian (e.g. Ubuntu) Dependencies
ca-certificates
fonts-liberation
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
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
安装依赖后,你需要使用此命令更新 nss
库
¥After installing dependencies you need to update nss
library using this
command
yum update nss -y
Check out discussions
chrome-headless-shell 禁用 GPU 合成
¥chrome-headless-shell disables GPU compositing
chrome-headless-shell 需要 --enable-gpu
到 在无头模式下启用 GPU 加速。
¥chrome-headless-shell requires --enable-gpu
to
enable GPU acceleration in headless mode.
const browser = await puppeteer.launch({
headless: 'shell',
args: ['--enable-gpu'],
});
使用 Chrome 设置 GPU
¥Setting up GPU with Chrome
一般来说,如果系统有合适的驱动程序,Chrome 应该能够检测并启用 GPU。有关其他提示,请参阅以下博客文章 https://developer.chrome.com/blog/supercharge-web-ai-testing。
¥Generally, Chrome should be able to detect and enable GPU if the system has appropriate drivers. For additional tips, see the following blog post https://developer.chrome.com/blog/supercharge-web-ai-testing.