初识Playwright
QingYuAlive Lv1

Playwright概述

Playwright是一个由Microsoft开发的开源自动化测试工具,专为满足端到端需求而创建,Playwright的API覆盖了所有现代浏览器,包括谷歌Chrome和微软Edge(Chrome内核)、苹果Safari(WebKit内核)和Mozilla Firefox,可在 Windows、Linux 和 macOS 上进行本地测试或在 CI 上进行测试,也可使用本地移动模拟进行无头测试或有头测试。

因为其弹性的页面元素选择器,Playwright可以依靠面向用户的字符串,如文本内容和label标签来选择元素,通过录制和回放用户操作,Playwright可以实现对网页的自动化操作,如点击、输入等,从而更准确地模拟真实用户的使用场景

Playwright安装

这里只介绍基于Python的Playwright安装

Pip安装

1
2
3
pip install --upgrade pip
pip install playwright
playwright install
  • pip install --upgrade pip:这个命令的目的是升级Python包管理工具pip本身

  • pip install playwright:这个命令用于通过pip安装Playwright Python库

  • playwright install:这个命令是Playwright的命令行工具,用于安装或更新浏览器

Conda安装

1
2
3
4
conda config --add channels conda-forge
conda config --add channels microsoft
conda install playwright
playwright install
  • conda config --add channels conda-forge:这个命令向Conda配置中添加了一个新的软件包通道(channel),即conda-forge
  • conda config --add channels microsoft:这个命令向Conda配置中添加了另一个软件包通道,即microsoft
  • conda install playwright:这个命令使用Conda来安装Playwright库

Playwright常用工具

Playwright脚本使用现有的调试工具,如Python调试器和浏览器开发者工具。除此之外,Playwright还未浏览器自动化引入了新的调试功能

Test Generator(测试生成器)

Playwright Test Generator是一个GUI工具,它帮助你了解测试中记录的Playwright的运行过程,这意味着你可以查看测试中的每一个操作,并直观的看到每个操作期间都发生了什么

1
playwright codegen <url>

Inspector(检查器)

Playwright Inspector是一个GUI工具,帮助编写和调试Playwright脚本,这也是默认推荐的脚本debug工具

  • 设置环境变量(powerShell)
1
$env:PWDEBUG=1
  • 运行测试脚本(debug模式)
1
pytest -s <被测试的Python文件名>
  • 还原环境变量(powerShell)
1
$env:PWDEBUG="console"

Trace Viewer(跟踪查看器)

Playwright Trace Viewer是一个GUI工具,用于帮助记录脚本运行时的产生的详细堆栈信息

  • 实例代码
1
2
3
4
5
6
7
8
9
10
11
browser = chromium.launch()
context = browser.new_context()

# Start tracing before creating / navigating a page.
context.tracing.start(screenshots=True, snapshots=True, sources=True)

page = context.new_page()
page.goto("https://playwright.dev")

# Stop tracing and export it into a zip archive.
context.tracing.stop(path = "trace.zip")

执行该Python脚本文件后,会在脚本所在目录生成一个trace.zip文件

  • 本地打开
1
playwright show-trace trace.zip

简单的网络爬虫

前一节我们介绍了较为实用的三个Playwright的内置工具,得益于Playwright的强大功能和较低的门槛,通过使用这些工具,我们能轻松的完成一些简单的网络爬虫的编写

以下是一个从某高考志愿分析网站获取各个大学信息的网络爬虫的例子

在终端使用codegen命令,此时Test Generator运行,打开了一个新的浏览器的空白页,我们输入网址,进入到该网站的院校库,之后我们关闭这个浏览器,Test Generator为我们生成了这样一段Python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
page.goto("https://m.youzy.cn/")
page.get_by_role("link", name="找大学").click()
page.frame_locator("iframe").get_by_text("大学排名").click()
page.frame_locator("iframe").get_by_role("feed").locator("div").filter(has_text="清华大学 985 211 国重点 保研 双一流 北京 海淀区 / 综合 / 公办 / 排名:1").first.click()
page.close()

# ---------------------
context.close()
browser.close()


with sync_playwright() as playwright:
run(playwright)

可以看到脚本文件中已经得到了一个用于记录清华大学信息的页面,我们只需使用适当的解析器进行解析,便能获取到其中包含的信息。此外,类比生成的代码,我们可以通过添加循环的方式获取到更多大学的页面以获取更多的数据,至此,我们便完成了一个简单的网络爬虫

总结

在这里,我们仅仅是简单地了解了Playwright的一些基本功能和作用,如同冰山一角,作为一项新兴的自动化测试工具,Playwright的生态和文档尚未达到完备,其许多强大的功能仍然等待着我们进一步的发掘

 Comments
Comment plugin failed to load
Loading comment plugin