python Playwright 教程
官方文档:https://playwright.dev/docs/intro
1、Playwright介绍
Playwright是一个强大的Python库,仅用一个API即可自动执行Chromium、Firefox、WebKit等主流浏览器自动化操作,并同时支持以无头模式、有头模式运行。
Playwright提供的自动化技术是绿色的、功能强大、可靠且快速,支持Linux、Mac以及Windows操作系统。
ps:博主以前都是用selenium来做ui自动化测试的, 部署的时候,还得额外安装一个本地浏览器,或者去制作一个绿色免安装版的浏览器(mac、linux还不知道怎么制作)非常的不喜欢。Playwright可以避免这种问题,Playwright安装后,直接是打开浏览器引擎Chromium、Firefox、WebKit进行驱动操作,绿色且快速。
二、安装
安装playwright库
pip install playwright
安装浏览器驱动文件(安装过程稍微有点慢)
python -m playwright install
安装成功后可查看帮助
python -m playwright codegen --help
Usage: playwright codegen [options] [url]
open page and generate code for user actions
Options:
-o, --output saves the generated script to a file
--target language to generate, one of javascript, test, python, python-async, pytest, csharp, java (default: "python")
--save-trace record a trace for the session and save it to a file
-b, --browser browser to use, one of cr, chromium, ff, firefox, wk, webkit (default: "chromium")
--block-service-workers block service workers
--channel Chromium distribution channel, "chrome", "chrome-beta", "msedge-dev", etc
--color-scheme emulate preferred color scheme, "light" or "dark"
--device emulate device, for example "iPhone 11"
--geolocation specify geolocation coordinates, for example "37.819722,-122.478611"
--ignore-https-errors ignore https errors
--load-storage load context storage state from the file, previously saved with --save-storage
--lang specify language / locale, for example "en-GB"
--proxy-server specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"
--proxy-bypass comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"
--save-har save HAR file with all network activity at the end
--save-har-glob filter entries in the HAR by matching url against this glob pattern
--save-storage save context storage state at the end, for later use with --load-storage
--timezone
帮助解析
options含义:
-o:将录制的脚本保存到一个文件
--target:规定生成脚本的语言,有JS和Python两种,默认为Python
-b:指定浏览器驱动
我们可以执行一行命令打开浏览器录制操作并生成脚本,例如:
python -m playwright codegen --target python -o 'demo.py' -b chromium https://www.baidu.com
录制关闭后,自动生成demo.py脚本,执行demo.py可以回放。(ps:滚动操作无法录制到,得自己修改代码,添加滚动操作)
demo.py
from playwright.sync_api import Playwright, sync_playwright, expect
def run(playwright: Playwright) -> None:
browser = playwright.chromium.launch(headless=False)
context = browser.new_context()
# Open new page
page = context.new_page()
# Go to https://www.baidu.com/
page.goto("https://www.baidu.com/")
# Click input[name="wd"]
page.locator("input[name=/"wd/"]").click()
# Fill input[name="wd"]
page.locator("input[name=/"wd/"]").fill("Playwright")
# Click text=百度一下
page.locator("text=百度一下").click()
page.wait_for_url("https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=Playwright&fenlei=256&rsv_pq=dc316e380004ede0&rsv_t=8fd9RWXb83JO2c9YXPfB1V6gwx%2F4Yt3sFpHTOQjLhfEwx3pjr%2Bkui28W74ox&rqlang=en&rsv_enter=0&rsv_dl=tb&rsv_sug3=2&rsv_sug1=1&rsv_sug7=100&rsv_btype=i&prefixsug=Playwright&rsp=0&inputT=1949&rsv_sug4=2154")
# Close page
page.close()
# ---------------------
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
Playwright的更多优点可查看官方文档 https://playwright.dev/docs/intro
共有 0 条评论