VS Code中npx playwright codegen无法运行及相关问题求助
解决Playwright Codegen的两个问题:VS Code终端TargetClosedError及无TypeScript选项
一、VS Code终端执行npx playwright codegen报TargetClosedError的解决办法
- 核对环境变量差异:在VS Code终端执行
echo %PATH%,和CMD里执行同命令的结果对比,把缺失的浏览器路径(比如Chrome、Edge的安装目录)添加到系统环境变量,重启VS Code后重试命令。 - 关闭终端集成:打开VS Code设置,搜索
terminal.integrated.shellIntegration.enabled,设为false,重启终端再运行npx playwright codegen。 - 强制指定浏览器:执行
npx playwright codegen --browser chrome(或firefox/edge),明确指定启动的浏览器,避免自动检测异常。 - 重装浏览器依赖:执行
npx playwright install --force,强制重新安装Playwright对应的浏览器驱动,清理损坏的缓存文件。
二、Playwright Inspector Target下拉无TypeScript选项的解决办法
- 初始化TypeScript项目:在项目根目录执行
npm init playwright@latest,选择TypeScript作为生成代码的语言,自动创建tsconfig.json和相关配置。如果已有项目,手动添加基础tsconfig.json:{ "compilerOptions": { "target": "ESNext", "module": "commonjs", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true } } - 启动时指定语言:在CMD中执行
npx playwright codegen --target typescript,强制指定输出TypeScript代码,此时Inspector的Target下拉会显示TypeScript选项。 - 升级Playwright版本:执行
npx playwright --version查看当前版本,若不是最新稳定版,执行npm install -D @playwright/test@latest升级,旧版本可能存在UI选项缺失的bug。
内容的提问来源于stack exchange,提问作者Anna Boekestyn
相关产品推荐
相关产品推荐

