使用 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,安装成功。

相关推荐
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
rockmelodies2 天前
# Windows Server2008 R2 Standard(SP1镜像U盘)重置本地管理员密码【完整详细步骤】
windows·密码破解
Winlifes2 天前
我给 Codex 做了一个 Git 面板:分支树、提交历史和工作区操作
git
kakakahahahaha2 天前
Windows C盘空间不足的安全清理与迁移流程
windows·电脑·笔记本电脑·软件需求·c盘清理
做咩啊~2 天前
远程连接提示:身份验证错误,要求的函数不受支持
windows
百事牛科技2 天前
PPT只读怎么取消?四种情况分别处理
windows·powerpoint
郑州光合科技余经理3 天前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
我命由我123453 天前
Git 推送报错:error: src refspec main does not match any
运维·git·gitee·github·运维开发·学习方法·版本控制
Lcr3s3 天前
Git基础之(0):如何在Ubuntu(Linux)上安装git
git
熊明才3 天前
WSL2 网络突然瘫痪(Network is unreachable / 没有 eth0)最短修复指南(2026年9月20日亲测可用)
linux·windows·wsl2