python Playwright 教程
2022-09-20 本文已影响0人
HC2
官方文档: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 <file name> saves the generated script to a file
--target <language> language to generate, one of javascript, test, python, python-async, pytest, csharp, java (default: "python")
--save-trace <filename> record a trace for the session and save it to a file
-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
帮助解析
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