1. 安装
要使用 Playwright,需要 Python 3.7 版本及以上
pip install playwright # 安装playwright的python版本 playwright install # 安装playwright自带的浏览器和ffmepg
离线安装
a. 先在在线环境安装完成,离线环境的python安装目录与在线环境一致
b. 将【C:\Python37\Lib\site-packages】路径下新更新的包,copy到线环境对应目录下
c. 将【C:\Python37\Scripts】路径下新更新的文件【playwright.exe】,copy到线环境对应目录下
d. 将【C:\Users\weiyt\AppData\Local】路径下新更新的文件夹【ms-playwright】,copy到线环境对应目录下
2. 录制
playwright codegen
3. 优化代码
视情况删除不必要代码,添加截图断言等
# 文本输入 page.fill("#kw", "playwright") # 页面点击 page.click("text=百度一下") page.click("#su") # 截图 page.screenshot(path='./pic/baidu.png') # 直接对比断言 assert page.title() == "百度一下,你就知道"
API文档: https://playwright.dev/python/docs/api/class-frame