官方文档: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 --helpUsage: playwright codegen [options] [url]open page and generate code for user actionsOptions: -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 commandExamples: $ 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, expectdef 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("laywright") # 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 |