Playwright是由微软公司2020年初发布的新一代自动化测试工具,相较于目前最常用的Selenium,它仅用一个API即可自动执行主流浏览器(Chromium、Firefox、WebKit)自动化操作。
安装
(venv)?lifeng@apple?bin?%?pip3?install?playwright
Collecting?playwright
??Downloading?playwright-1.17.0-py3-none-macosx_10_13_x86_64.whl?(29.6?MB)
?????|████████████████████████████████|?29.6?MB?26?kB/s?????????????
Collecting?pyee>=8.0.1
??Downloading?pyee-8.2.2-py2.py3-none-any.whl?(12?kB)
Collecting?websockets>=8.1
??Downloading?websockets-10.1-cp39-cp39-macosx_10_9_x86_64.whl?(96?kB)
?????|████████████████████████████████|?96?kB?257?kB/s????????????
Requirement?already?satisfied:?greenlet>=1.0.0?in?/Users/lifeng/python-virtualenv/venv/lib/python3.9/site-packages?(from?playwright)?(1.1.2)
Installing?collected?packages:?websockets,?pyee,?playwright
Successfully?installed?playwright-1.17.0?pyee-8.2.2?websockets-10.1
安装好playwright 库以后,再安装相应的浏览器:
(venv)?lifeng@apple?bin?%?playwright?install?chromium
Downloading?Playwright?build?of?chromium?v939194?-?119.9?Mb?[=======]?100%?0.0s
Playwright?build?of?chromium?v939194?downloaded?to?/Users/lifeng/Library/Caches/ms-playwright/chromium-939194
Downloading?Playwright?build?of?ffmpeg?v1006?-?1.1?Mb?[=============]?100%?0.0s
Playwright?build?of?ffmpeg?v1006?downloaded?to?/Users/lifeng/Library/Caches/ms-playwright/ffmpeg-1006
(venv)?lifeng@apple?bin?%?playwright?install?firefox?
Downloading?Playwright?build?of?firefox?v1304?-?71.7?Mb?[===========]?100%?0.0s
Playwright?build?of?firefox?v1304?downloaded?to?/Users/lifeng/Library/Caches/ms-playwright/firefox-1304
(venv)?lifeng@apple?bin?%?playwright?install?webkit?
Downloading?Playwright?build?of?webkit?v1578?-?54.2?Mb?[============]?100%?0.0s
Playwright?build?of?webkit?v1578?downloaded?to?/Users/lifeng/Library/Caches/ms-playwright/webkit-1578
Webkit 是一个开放源码的浏览器引擎 (web browser engine) ,基于 WebKit 的浏览器产品有:苹果的 Safari 和 iPhone , Google 的 Chrome 和 Android , Nokia 的 S60 ,傲游 3(Maxthon3) ;
用法
安装后,您可以import在 Python 脚本中使用 Playwright,并启动 3 种浏览器(chromium、firefox和webkit)中的任何一种。
from?playwright.sync_api?import?sync_playwright
with?sync_playwright()?as?p:
????#?browser?=?p.webkit.launch()
????browser?=?p.firefox.launch()
????#?browser?=?p.chromium.launch()
????page?=?browser.new_page()
????page.goto("http://www.baidu.com")
????print(page.title())
????browser.close()
Playwright库 支持 API 的两种变体:同步和异步。如果您的现代项目使用asyncio,您应该使用 async API:
import?asyncio
from?playwright.async_api?import?async_playwright
async?def?main():
????async?with?async_playwright()?as?p:
????????#?browser?=?await?p.webkit.launch()
????????#?browser?=?await?p.firefox.launch()
????????browser?=?await?p.chromium.launch()
????????page?=?await?browser.new_page()
????????await?page.goto("https://www.baidu.com")
????????print(await?page.title())
????????await?browser.close()
两种运行结果显示如下:
百度一下,你就知道
Process?finished?with?exit?code?0
第一个脚本
from?playwright.sync_api?import?sync_playwright
with?sync_playwright()?as?p:
????browser?=?p.webkit.launch()
????page?=?browser.new_page()
????page.goto("https://www.baidu.com")
????page.screenshot(path="example.png")
????browser.close()
使用webkit 浏览器访问百度,然后进行截图,图片名称命名为example.png 。
录制脚本
(venv)?lifeng@apple?~?%?playwright?codegen?baidu.com?
最后把录制视频的脚本复制下来,放到你的编辑器中去运行即可:
from?playwright.sync_api?import?Playwright,?sync_playwright
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.click("input[name=\"wd\"]")
????#?Fill?input[name="wd"]
????page.fill("input[name=\"wd\"]",?"")
????#?Press?CapsLock
????page.press("input[name=\"wd\"]",?"CapsLock")
????#?Fill?input[name="wd"]
????page.fill("input[name=\"wd\"]",?"百度百科")
????#?Press?Enter
????#?with?page.expect_navigation(url="https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E7%99%BE%E5%BA%A6%E7%99%BE%E7%A7%91&fenlei=256&rsv_pq=d14d33e400031dcf&rsv_t=8855fje1bY92gcAswotHpKJo1fjO0owlbzvdv6r4R20TSrzxKJlDNInu2LE&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=18&rsv_sug1=15&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&prefixsug=%25E7%2599%25BE%25E5%25BA%25A6%25E7%2599%25BE%25E7%25A7%2591&rsp=5&inputT=5076&rsv_sug4=5769&rsv_jmp=fail"):
????with?page.expect_navigation():
????????page.press("input[name=\"wd\"]",?"Enter")
????#?assert?page.url?==?"https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=%E7%99%BE%E5%BA%A6%E7%99%BE%E7%A7%91&fenlei=256&rsv_pq=d14d33e400031dcf&rsv_t=8855fje1bY92gcAswotHpKJo1fjO0owlbzvdv6r4R20TSrzxKJlDNInu2LE&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=18&rsv_sug1=15&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&prefixsug=%25E7%2599%25BE%25E5%25BA%25A6%25E7%2599%25BE%25E7%25A7%2591&rsp=5&inputT=5076&rsv_sug4=5769"
????#?Click?text=百度百科
????#?with?page.expect_navigation(url="https://baike.baidu.com/"):
????with?page.expect_navigation():
????????with?page.expect_popup()?as?popup_info:
????????????page.click("text=百度百科")
????????page1?=?popup_info.value
????#?---------------------
????context.close()
????browser.close()
with?sync_playwright()?as?playwright:
????run(playwright)
playwright和selenium对比
| playwright | selenium |
---|
语言 | 主流语言 | 主流语言 | 浏览器 | 谷歌/火狐/Webkit | 市场主流浏览器(除去套皮浏览器) | 平台 | 跨平台 | 跨平台 | 驱动 | 不需要 | 需要更新驱动 | 运行速度 | 快 | 慢一点 | 脚本录制 | 有(强一些) | 有(相对弱一些) | 脚本稳定性 | 待测 | 需要加等待机制 | 文档社区 | 较少 | 丰富 |
以上就是初步了解的对比区分,但感觉playwright 的前景还可以的,后续会写一些系列文章,大家共同讨论学习。
以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你,如有疑问、歧义,直接私信留言会及时修正发布;非常期待你的点赞和分享哟,谢谢!
未完,待续…
一直都在努力,希望您也是!
微信搜索公众号:就用python
文章作者:李 锋,编辑排版:梁莉莉
|