Playwright 简介

Playwright 安装指南(2025 年最新版)

Playwright 支持多种语言,以下重点介绍最常用的 PythonNode.js/JavaScript/TypeScript 版本的安装步骤。官方最新版本为 1.57.0(2025 年 12 月)。安装后会自动下载 Chromium、Firefox 和 WebKit 浏览器驱动。

1. Python 版安装(推荐新手,简单易用)

要求:Python 3.8 或更高版本。

步骤

  1. 安装 Playwright 库
   pip install playwright
  • 如果网络慢,可使用国内镜像:
    bash pip install playwright -i https://mirrors.aliyun.com/pypi/simple/
  1. 安装浏览器驱动(必须执行,否则无法运行):
   playwright install
  • 默认下载 Chromium、Firefox 和 WebKit。如果只想安装特定浏览器:
    bash playwright install chromium # 只安装 Chromium playwright install firefox webkit # 指定
  1. 验证安装
   playwright --version

或运行一个简单脚本测试。

Conda 用户(可选):

conda config --add channels conda-forge
conda config --add channels microsoft
conda install playwright
playwright install

2. Node.js 版安装(推荐测试框架使用)

要求:Node.js 最新 20.x、22.x 或 24.x 版本。

步骤

  1. 初始化项目并安装(推荐一键初始化,自动创建配置):
   npm init playwright@latest
  • 这会创建 playwright.config.ts 文件、测试目录,并安装浏览器。
  1. 手动安装(现有项目):
   npm i -D @playwright/test
   npx playwright install
  1. 验证安装
   npx playwright --version

VS Code 集成(强烈推荐):

  • 安装 Microsoft 官方 Playwright Test 扩展。
  • 打开命令面板(Ctrl+Shift+P),运行 “Test: Install Playwright” 选择浏览器。

3. 常见问题与解决

  • 网络问题(国内常见):使用代理或镜像;浏览器下载失败时,可手动重试 playwright install
  • 权限问题:Windows/macOS 用管理员/终端运行。
  • 更新 Playwright
  npm i -D @playwright/test@latest  # Node.js
  pip install --upgrade playwright  # Python
  npx playwright install  # 重新下载浏览器
  • 系统要求:Windows 11+、macOS 12+、Linux(Debian/Ubuntu)。

安装完成后,你可以运行官方示例测试或用 npx playwright codegen 录制操作生成代码。更多详情参考官方文档:

  • Node.js:https://playwright.dev/docs/intro
  • Python:https://playwright.dev/python/docs/intro
  • 中文文档:https://playwright.nodejs.cn/ 或 https://playwright.net.cn/

如果需要特定语言的示例代码或遇到问题,提供更多细节我可以帮忙调试!

文章已创建 3420

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部