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

相关推荐
全栈陈序员1 天前
【Git】从入门到精通(五)—— git add:将文件加入暂存区
大数据·git·elasticsearch
全栈陈序员1 天前
【Git】从入门到精通(七)—— git status:查看仓库状态
大数据·git·elasticsearch
莫晓燚1 天前
Ansible 13 个常用模块---企业实战
git·github
中二骚年1 天前
Windows 安全中心 / 防火墙故障,calc和Microsoft Store,终端(管理员)无法正常打开。 原因 + 修复方案
windows·安全·microsoft
万联WANFLOW1 天前
CVE-2026-98432 等漏洞自查:AI 查出的 Linux、Windows 漏洞,运维怎么确认受不受影响
linux·运维·windows·网络安全
AlanAshley2 天前
Windows 中导致延迟的软硬件及协议比重
windows
yuezhilangniao2 天前
win10中文 以及 PowerShell 开发日常
windows·powershell
无言听风雨丶2 天前
Kubernetes GitOps 持续交付实战:用 Argo CD 让 Git 成为集群事实源
git·elasticsearch·kubernetes
全栈陈序员2 天前
【Git】从入门到精通(六)—— git commit:提交到版本库
大数据·git·elasticsearch
共享家95272 天前
408 操作系统第一章
服务器·数据库·windows