@puppeteer/browsers
通过 CLI 或编程方式管理并启动浏览器/驱动程序。
系统要求
- 兼容的 Node 版本(参见
package.json中的engines)。 - 对于 Firefox 下载:
- Linux 构建:解压
.tar.gz和.tar.bz2归档文件需要xz和bzip2工具。 - MacOS 构建:解压
.dmg归档文件需要hdiutil。
- Linux 构建:解压
- 对于 Chrome 下载:
- 在 Linux/MacOS 上:
unzip。 - 在 Windows 上:
tar.exe。
- 在 Linux/MacOS 上:
CLI
使用 npx 运行 CLI:
# This will install and run the @puppeteer/browsers package.
# If it is already installed in the current directory, the installed
# version will be used.
npx @puppeteer/browsers --help
内置的逐命令 help 会提供使用 CLI 所需的全部文档。
npx @puppeteer/browsers --help # help for all commands
npx @puppeteer/browsers install --help # help for the install command
npx @puppeteer/browsers launch --help # help for the launch command
npx @puppeteer/browsers clear --help # help for the clear command
npx @puppeteer/browsers list --help # help for the list command
使用 npx 时,你可以指定 @puppeteer/browsers 的版本:
# Always install and use the latest version from the registry.
npx @puppeteer/browsers@latest --help
# Always use a specific version.
npx @puppeteer/browsers@2.4.1 --help
# Always install the latest version and automatically confirm the installation.
npx --yes @puppeteer/browsers@latest --help
要清除所有已安装的浏览器,请使用 clear 命令:
npx @puppeteer/browsers clear
要列出所有已安装的浏览器,请使用 list 命令:
npx @puppeteer/browsers list
以下示例可帮助你了解 CLI 的用法(更多示例请使用 --help 命令):
# Download the latest available Chrome for Testing binary corresponding to the Stable channel.
npx @puppeteer/browsers install chrome@stable
# Download a specific Chrome for Testing version.
npx @puppeteer/browsers install chrome@116.0.5793.0
# Download the latest Chrome for Testing version for the given milestone.
npx @puppeteer/browsers install chrome@117
# Download the latest available ChromeDriver version corresponding to the Canary channel.
npx @puppeteer/browsers install chromedriver@canary
# Download a specific ChromeDriver version.
npx @puppeteer/browsers install chromedriver@116.0.5793.0
# On Ubuntu/Debian and only for Chrome, install the browser and required system dependencies.
# If the browser version has already been installed, the command
# will still attempt to install system dependencies.
# Requires root privileges.
npx puppeteer browsers install chrome --install-deps
已知限制
- 只有 Chrome/Chromium 可以启动系统浏览器。
代理
该库和 CLI 遵循 HTTP_PROXY、HTTPS_PROXY 和 NO_PROXY 环境变量。要让它们生效,你必须安装 proxy-agent 包:
npm install proxy-agent
调试
要为 @puppeteer/browsers 操作启用详细日志(如下载进度、安装步骤和启动参数),请使用 Node.js 内置的 NODE_DEBUG 环境变量。
env NODE_DEBUG="puppeteer:browsers:*" npx @puppeteer/browsers install chrome@stable
以下调试通道可用:
puppeteer:browsers:cache:缓存操作。puppeteer:browsers:fileUtil:解压及其他文件工具操作。puppeteer:browsers:install:下载和安装进度。puppeteer:browsers:launcher:浏览器启动参数和进程状态。
自定义提供程序
你可以实现自定义浏览器提供程序,从替代源下载,例如公司镜像、私有仓库或专用的浏览器构建。
import {
BrowserProvider,
DownloadOptions,
Browser,
BrowserPlatform,
} from '@puppeteer/browsers';
class SimpleMirrorProvider implements BrowserProvider {
constructor(private mirrorUrl: string) {}
supports(options: DownloadOptions): boolean {
return options.browser === Browser.CHROME;
}
getDownloadUrl(options: DownloadOptions): URL | null {
const {buildId, platform} = options;
const filenameMap = {
[BrowserPlatform.LINUX]: 'chrome-linux64.zip',
[BrowserPlatform.MAC]: 'chrome-mac-x64.zip',
[BrowserPlatform.MAC_ARM]: 'chrome-mac-arm64.zip',
[BrowserPlatform.WIN32]: 'chrome-win32.zip',
[BrowserPlatform.WIN64]: 'chrome-win64.zip',
};
const filename = filenameMap[platform];
if (!filename) return null;
return new URL(`${this.mirrorUrl}/chrome/${buildId}/${filename}`);
}
getExecutablePath(options: DownloadOptions): string {
const {platform} = options;
if (
platform === BrowserPlatform.MAC ||
platform === BrowserPlatform.MAC_ARM
) {
return 'chrome-mac/Chromium.app/Contents/MacOS/Chromium';
} else if (platform === BrowserPlatform.LINUX) {
return 'chrome-linux64/chrome';
} else if (platform.includes('win')) {
return 'chrome-win64/chrome.exe';
}
throw new Error(`Unsupported platform: ${platform}`);
}
}
与 install API 一起使用:
import {install} from '@puppeteer/browsers';
const customProvider = new SimpleMirrorProvider('https://internal.company.com');
await install({
browser: Browser.CHROME,
buildId: '120.0.6099.109',
platform: BrowserPlatform.LINUX,
cacheDir: '/tmp/puppeteer-cache',
providers: [customProvider],
});
多个提供程序可以串联使用——它们会按顺序依次尝试,直到其中一个成功,并以默认提供程序(如 Chrome for Testing)作为自动回退。
Puppeteer 不官方支持自定义提供程序。你需要对二进制兼容性、测试和维护承担全部责任。
API
编程式 API 允许你从代码中安装和启动浏览器。有关如何使用 install、canInstall、launch、computeExecutablePath、computeSystemExecutablePath 等方法,请参阅 test 文件夹中的示例。
类
|
类 |
说明 |
|---|---|
|
使用默认源的默认提供程序实现。这是 Puppeteer 使用的标准提供程序。 | |
|
备注: 此类的构造函数被标记为内部。第三方代码不应直接调用构造函数,也不应创建继承 | |
|
备注: 此类的构造函数被标记为内部。第三方代码不应直接调用构造函数,也不应创建继承 |
枚举
|
枚举 |
说明 |
|---|---|
|
受支持的浏览器。 | |
|
用于标识操作系统平台与架构组合的平台名称,其标识方式与浏览器下载相关。 | |
|
描述浏览器发布渠道的枚举。 你可以将其与 resolveBuildId() 结合使用,根据发布渠道解析构建 ID。 | |
函数
|
函数 |
说明 |
|---|---|
|
用于构建标准归档文件名的工具函数。 | |
|
通过检查已知安装位置(使用 https://pptr.dev/browsers-api/browsers.computesystemexecutablepath),根据发布渠道名称返回系统级 Chrome 安装的路径。如果在预期路径上未找到 Chrome 实例,则会抛出错误。 | |
|
获取用于下载指定浏览器二进制归档的 URL。 该归档绑定到指定的具体平台和构建 ID。 | |
|
返回缓存目录中已安装浏览器的元数据。 | |
|
返回指定浏览器的版本比较器,可用于对浏览器版本进行排序。 | |
|
根据 InstallOptions 下载并解压浏览器归档。 | |
|
根据 InstallOptions 下载浏览器归档,但不解压。 | |
|
根据 LaunchOptions 启动浏览器进程。 | |
|
返回给定渠道的预期默认用户数据目录。它不会检查该目录是否实际存在。 | |
接口
|
接口 |
说明 |
|---|---|
|
自定义浏览器提供程序实现的接口。允许用户为浏览器实现替代下载源。 ⚠️ 重要:Puppeteer 不官方支持自定义提供程序。 实现此接口即表示你接受以下全部责任:
Puppeteer 仅测试并保证 Chrome for Testing 二进制文件。 | |
|
传递给提供程序的选项。 | |
变量
|
变量 |
说明 |
|---|---|