【Python】Playwright模块进行自动化测试

playwright是由微软开发的Web UI自动化测试工具,支持Node.js、Python、C# 和 Java语言,本文将介绍Python版本的Playwright使用方法。

微软开源了一个非常强大的自动化项目叫playwright-python,项目地址:https://github.com/microsoft/playwright-python

它支持主流的浏览器,包含:Chrome、Firefox、Safari、Microsoft Edge 等,同时支持以无头模式、有头模式运行,并提供了同步、异步的 API,

可以结合 Pytest 测试框架使用,并且支持浏览器端的自动化脚本录制。

Playwright提供的自动化技术是绿色的、功能强大、可靠且快速,支持Linux、Mac以及Windows操作系统。
官网: https://playwright.dev/

从官网的解释,官方给Playwright定位是一款真正意义上的Web端到端测试工具。

安装步骤:

第 1 步,安装 playwright-python 依赖库 (需要注意的是,playwright库需要依赖Python3.7+以上)

bash 复制代码
pip install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade Playwright

第 2 步,安装主流的浏览器驱动

bash 复制代码
playwright install

如果想查看Playwright支持的功能, 可以直接在命令行输入:playwright -h

使用Playwright可以无需写代码,只需手动操作浏览器,它会录制我们的操作,然后自动生成代码脚本。
录制脚本:

  1. 查看录制命令说明 playwright codegen -h
bash 复制代码
Usage: playwright codegen [options] [url]

open page and generate code for user actions

Options:
  -o, --output <file name>             saves the generated script to a file
  --target <language>                  language to generate, one of javascript, playwright-test, python, python-async,
                                       python-pytest, csharp, csharp-mstest, csharp-nunit, java (default: "python")
  --save-trace <filename>              record a trace for the session and save it to a file
  --test-id-attribute <attributeName>  use the specified attribute to generate data test ID selectors
  -b, --browser <browserType>          browser to use, one of cr, chromium, ff, firefox, wk, webkit (default:
                                       "chromium")
  --block-service-workers              block service workers
  --channel <channel>                  Chromium distribution channel, "chrome", "chrome-beta", "msedge-dev", etc
  --color-scheme <scheme>              emulate preferred color scheme, "light" or "dark"
  --device <deviceName>                emulate device, for example  "iPhone 11"
  --geolocation <coordinates>          specify geolocation coordinates, for example "37.819722,-122.478611"
  --ignore-https-errors                ignore https errors
  --load-storage <filename>            load context storage state from the file, previously saved with --save-storage
  --lang <language>                    specify language / locale, for example "en-GB"
  --proxy-server <proxy>               specify proxy server, for example "http://myproxy:3128" or
                                       "socks5://myproxy:8080"
  --proxy-bypass <bypass>              comma-separated domains to bypass proxy, for example
                                       ".com,chromium.org,.domain.com"
  --save-har <filename>                save HAR file with all network activity at the end
  --save-har-glob <glob pattern>       filter entries in the HAR by matching url against this glob pattern
  --save-storage <filename>            save context storage state at the end, for later use with --load-storage
  --timezone <time zone>               time zone to emulate, for example "Europe/Rome"
  --timeout <timeout>                  timeout for Playwright actions in milliseconds, no timeout by default
  --user-agent <ua string>             specify user agent string
  --viewport-size <size>               specify browser viewport size in pixels, for example "1280, 720"
  -h, --help                           display help for command

Examples:

  $ codegen
  $ codegen --target=python
  $ codegen -b webkit https://example.com

比如,我要在baidu.com搜索,用chromium驱动,将结果保存为test.py的python文件。文档默认保存到 C:\Users\user\下面

bash 复制代码
playwright codegen --target python -o 'test.py' -b chromium https://www.baidu.com
相关推荐
郝学胜-神的一滴4 分钟前
Qt 高级开发 011: 跨线程信号槽实战
开发语言·c++·qt·程序人生·开源软件·用户界面
xingyuzhisuan4 分钟前
2026实测:租用RTX 4090 CUDA适配与PyTorch精准安装教程
人工智能·pytorch·python·深度学习·gpu算力
嗯、.7 分钟前
Agent 路由架构的一次尝试:LangGraph + Swarm Handoff + 小模型 Router
人工智能·python·swarm·langgraph·multi-agent·model-routing
轻刀快马8 分钟前
讲透分布式系统的演进史与核心架构
开发语言·架构·php
学困昇8 分钟前
Linux 动静态库制作与原理:从 .a、.so 到 ELF 加载一次讲透
linux·运维·服务器·c语言·开发语言·c++·人工智能
SunnyDays101110 分钟前
Java 实现插入和删除 Excel 行和列
java·python·excel
kels889911 分钟前
加密货币实时api的订单簿快照多久更新一次?
开发语言·笔记·python·金融·区块链
Byte Wizard13 分钟前
C语言数据在内存中的存储
c语言·开发语言
落日屿星辰13 分钟前
PyTorch 模型迁移到昇腾NPU 完整指南
人工智能·pytorch·python
basketball61615 分钟前
C++面试考点 头文件与实现文件形式
开发语言·c++