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

相关推荐
保卫大狮兄8 小时前
2026中小制造企业轻量化ERP选型观察:产供销协同路线、上线周期与ROI分析
windows·microsoft·erp·生产管理
陈工大模型9 小时前
MacOS 跨平台压缩包处理:从编码痛点到工程化落地的完整方案
windows·macos·缓存·电脑·策略模式·c盘清理
崖边看雾19 小时前
Git 手册:三区、分支、撤销、冲突与团队协作
大数据·git·elasticsearch
李永奉19 小时前
Git和GitHub-VS Code 图形化 Git 放弃更改、暂存与取消暂存、撤销上一次提交(VS Code 界面操作 + 命令行对照)
git·github
凌镜icecraft20 小时前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
the3clipse1 天前
Windows 11 26H2 AI 功能使用指南:回顾、单击以执行与实时字幕
人工智能·windows·copilot+ pc·recall 回顾·单击以执行·实时字幕翻译·windows studio
aifannihao1 天前
断言从 == 改成 > 0:用 Python 审出 git diff 里的测试降级
git·python·测试·标准库·代码审查
the3clipse1 天前
Windows 11 26H2 账户登录与 Windows Hello 指南:并行验证、增强登录安全与无密码设置
windows·无密码登录·windows hello·增强登录安全性ess·pin与密码区别·动态锁·win11 26h2
承渊政道1 天前
Toonflow AI短剧教程:Windows部署、批量生成与角色一致性
人工智能·windows·cpolar·ai短剧·toonflow
李永奉1 天前
Git和GitHub-VS Code 图形化 Git初始化、状态标识、提交、关联远程仓库与首次推送(全程在 VS Code 内完成,每步附对应Git命令)
git·github