使用 OpenCode 在 Windows 上加速安装 Playwright 的完整指南

问题分析

在使用OpenCode时,Playwright浏览器自动化工具是必不可少的组件。然而在国内网络环境下,npx playwright install下载浏览器二进制文件速度极慢,经常卡在0%。本文将详细介绍如何通过国内镜像加速安装。

Playwright v1.58+之后,Chromium下载切换为Chrome for Testing(CFT),URL路径变为builds/cft/前缀,导致国内镜像(如npmmirror)路径不匹配,出现404错误。

解决方案:手动下载+离线安装

第一步:确认版本

运行以下命令查看需要的Chromium版本:

复制代码
npx playwright install chromium --dry-run

第二步:从npmmirror下载

根据第一步得到的版本号,从国内镜像下载:

复制代码
https://cdn.npmmirror.com/binaries/chrome-for-testing/148.0.7778.96/win64/chrome-win64.zip

第三步:解压到缓存目录

将下载的zip解压到Playwright的缓存目录,并创建安装完成标记:

复制代码
$installDir = "$env:LOCALAPPDATA\ms-playwright\chromium-1223"
New-Item -ItemType Directory -Path "$installDir\chrome-win64" -Force
Expand-Archive -Path "D:\chrome-win64.zip" -DestinationPath "$installDir\" -Force
Set-Content -Path "$installDir\INSTALLATION_COMPLETE" -Value ""

验证

再次运行安装命令:

复制代码
npx playwright install chromium

附:实际安装过程记录

1. 环境准备

  • 项目目录:D:\project\opencode
  • Windows + PowerShell(执行策略限制,改用 cmd /c

2. 安装依赖

复制代码
cmd /c "npm install @playwright/test"

3. 查看所需版本

复制代码
cmd /c "npx playwright install chromium --dry-run"
  • Chromium revision: 1223
  • Chrome 版本: 148.0.7778.96
  • 安装路径: C:\Users\username\AppData\Local\ms-playwright\chromium-1223

4. 从国内镜像下载

官方 CDN 极慢,改用 npmmirror 镜像:

复制代码
https://cdn.npmmirror.com/binaries/chrome-for-testing/148.0.7778.96/win64/chrome-win64.zip

约 182 MB,用 Invoke-WebRequest 下载。

5. 离线安装

复制代码
$installDir = "$env:LOCALAPPDATA\ms-playwright\chromium-1223"
New-Item -ItemType Directory -Path "$installDir\chrome-win64" -Force
Expand-Archive -Path "D:\chrome-win64.zip" -DestinationPath "$installDir\" -Force
Set-Content -Path "$installDir\INSTALLATION_COMPLETE" -Value ""

**关键:**目录结构必须为:

复制代码
chromium-1223\
  ├── chrome-win64\chrome.exe
  └── INSTALLATION_COMPLETE

6. Headless Shell 同样方式安装

复制代码
https://cdn.npmmirror.com/binaries/chrome-for-testing/148.0.7778.96/win64/chrome-headless-shell-win64.zip

7. 验证

复制代码
cmd /c "npx playwright install chromium"

Playwright 跳过已安装的 Chromium,只下载 FFmpeg 和 Winldd,安装成功。

相关推荐
kyriewen9 小时前
别再每次都 Google 了:我整理了前端日常最常踩的 10 个 Git 坑,附速查表
前端·javascript·git
A_Lonely_Cat3 天前
记一次 GitHub 幽灵协作者大清洗:强制重写 Git 历史与穿透 CDN 缓存实践
git·github
和你看星星5 天前
Git rerere:让重复冲突只解决一次
git
嘻嘻仙人8 天前
Ubuntu中 git上传自己的项目和二次上传一般流程
git·github
Patrick_Wilson8 天前
Squash Merge 的血缘陷阱:为什么删掉的代码又活了过来
前端·git·程序员
沉浸学习的匿名网友8 天前
什么是 .gitignore?为什么每个 Git 项目几乎都离不开它?
前端·git
深海鱼在掘金9 天前
Git 完全指南 —— 第3章:理解工作区、暂存区、版本库三个核心
git
江华森9 天前
Git 基础筑基:从原理到团队协作的全栈实战
git
JakeJiang10 天前
Git 必备命令指南:从日常高频到项目开发实战
git
叫我少年10 天前
Windows 中安装 git
git