摘要:是由微軟公司年初發(fā)布的新一代自動(dòng)化測(cè)試工具,相較于目前最常用的,它僅用一個(gè)即可自動(dòng)執(zhí)行主流瀏覽器自動(dòng)化操作。庫(kù)支持的兩種變體同步和異步。
Playwright是由微軟公司2020年初發(fā)布的新一代自動(dòng)化測(cè)試工具,相較于目前最常用的Selenium,它僅用一個(gè)API即可自動(dòng)執(zhí)行主流瀏覽器(Chromium、Firefox、WebKit)自動(dòng)化操作。
(venv)?lifeng@apple?bin?%?pip3?install?playwrightCollecting?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,?playwrightSuccessfully?installed?playwright-1.17.0?pyee-8.2.2?websockets-10.1
安裝好playwright
庫(kù)以后,再安裝相應(yīng)的瀏覽器:
(venv)?lifeng@apple?bin?%?playwright?install?chromiumDownloading?Playwright?build?of?chromium?v939194?-?119.9?Mb?[=======]?100%?0.0sPlaywright?build?of?chromium?v939194?downloaded?to?/Users/lifeng/Library/Caches/ms-playwright/chromium-939194Downloading?Playwright?build?of?ffmpeg?v1006?-?1.1?Mb?[=============]?100%?0.0sPlaywright?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.0sPlaywright?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.0sPlaywright?build?of?webkit?v1578?downloaded?to?/Users/lifeng/Library/Caches/ms-playwright/webkit-1578
Webkit 是一個(gè)開(kāi)放源碼的瀏覽器引擎 (web browser engine) ,基于 WebKit 的瀏覽器產(chǎn)品有:蘋(píng)果的 Safari 和 iPhone , Google 的 Chrome 和 Android , Nokia 的 S60 ,傲游 3(Maxthon3) ;
安裝后,您可以import在 Python 腳本中使用 Playwright,并啟動(dòng) 3 種瀏覽器(chromium、firefox和webkit)中的任何一種。
from?playwright.sync_api?import?sync_playwrightwith?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庫(kù) 支持 API 的兩種變體:同步和異步。如果您的現(xiàn)代項(xiàng)目使用asyncio,您應(yīng)該使用 async API:
import?asynciofrom?playwright.async_api?import?async_playwrightasync?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()
兩種運(yùn)行結(jié)果顯示如下:
百度一下,你就知道Process?finished?with?exit?code?0
from?playwright.sync_api?import?sync_playwrightwith?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
瀏覽器訪問(wèn)百度,然后進(jìn)行截圖,圖片名稱命名為example.png
。
(venv)?lifeng@apple?~?%?playwright?codegen?baidu.com?
最后把錄制視頻的腳本復(fù)制下來(lái),放到你的編輯器中去運(yùn)行即可:
from?playwright.sync_api?import?Playwright,?sync_playwrightdef?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 | |
---|---|---|
語(yǔ)言 | 主流語(yǔ)言 | 主流語(yǔ)言 |
瀏覽器 | 谷歌/火狐/Webkit | 市場(chǎng)主流瀏覽器(除去套皮瀏覽器) |
平臺(tái) | 跨平臺(tái) | 跨平臺(tái) |
驅(qū)動(dòng) | 不需要 | 需要更新驅(qū)動(dòng) |
運(yùn)行速度 | 快 | 慢一點(diǎn) |
腳本錄制 | 有(強(qiáng)一些) | 有(相對(duì)弱一些) |
腳本穩(wěn)定性 | 待測(cè) | 需要加等待機(jī)制 |
文檔社區(qū) | 較少 | 豐富 |
以上就是初步了解的對(duì)比區(qū)分,但感覺(jué)playwright
的前景還可以的,后續(xù)會(huì)寫(xiě)一些系列文章,大家共同討論學(xué)習(xí)。
以上總結(jié)或許能幫助到你,或許幫助不到你,但還是希望能幫助到你,如有疑問(wèn)、歧義,直接私信留言會(huì)及時(shí)修正發(fā)布;非常期待你的點(diǎn)贊和分享喲,謝謝!
未完,待續(xù)…
一直都在努力,希望您也是!
微信搜索公眾號(hào):就用python
文章作者:李 鋒,編輯排版:梁莉莉
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/124435.html
摘要:在我的案例中,我看到谷歌正在扼殺他們的短鏈接服務(wù),而最流行的縮短器擴(kuò)展是使用谷歌的縮短器,并且有超過(guò)萬(wàn)用戶。 Csaba經(jīng)驗(yàn)分享 2、Http Server Online:網(wǎng)頁(yè)版HTTP服務(wù) 今天看到一個(gè)思路很奇特的開(kāi)源小項(xiàng)目,網(wǎng)頁(yè)版實(shí)現(xiàn)加載http服務(wù)。一般我們會(huì)啟動(dòng)一個(gè)http server來(lái)實(shí)現(xiàn)http的項(xiàng)目啟動(dòng)加載,有Node.js、Java、Python等等。通過(guò)...
摘要:所以,應(yīng)該直接了當(dāng)?shù)陌惭b,其中也會(huì)自動(dòng)安裝上,作為其運(yùn)行的。這只是一開(kāi)始,之后還有里各種找不到外部安裝的的情況。終于,意識(shí)到這些方法都是錯(cuò)誤的思路。這才知道原來(lái)是無(wú)法識(shí)別。 為什么要用IPython/Jupyter? python里面調(diào)試確實(shí)有點(diǎn)煩惱,尤其是在vim里,想要嘗試一些簡(jiǎn)單的編碼問(wèn)題,實(shí)在是有點(diǎn)麻煩,不想到命令行模式一行一行執(zhí)行,也不想再新建一個(gè)文件測(cè)試一個(gè)簡(jiǎn)單的功能。 而...
摘要:所以,應(yīng)該直接了當(dāng)?shù)陌惭b,其中也會(huì)自動(dòng)安裝上,作為其運(yùn)行的。這只是一開(kāi)始,之后還有里各種找不到外部安裝的的情況。終于,意識(shí)到這些方法都是錯(cuò)誤的思路。這才知道原來(lái)是無(wú)法識(shí)別。 為什么要用IPython/Jupyter? python里面調(diào)試確實(shí)有點(diǎn)煩惱,尤其是在vim里,想要嘗試一些簡(jiǎn)單的編碼問(wèn)題,實(shí)在是有點(diǎn)麻煩,不想到命令行模式一行一行執(zhí)行,也不想再新建一個(gè)文件測(cè)試一個(gè)簡(jiǎn)單的功能。 而...
摘要:官方網(wǎng)站介紹是一個(gè)輕量級(jí)的跨平臺(tái)測(cè)試自動(dòng)化工具,可以使用不同的語(yǔ)言中編寫(xiě)測(cè)試用例的能力。本質(zhì)上是一個(gè)行為驅(qū)動(dòng)開(kāi)發(fā)測(cè)試框架。支持測(cè)試報(bào)告可以生成格式的報(bào)告。 官方網(wǎng)站:https://docs.gauge.org/latest... 介紹: Gauge是一個(gè)輕量級(jí)的跨平臺(tái)測(cè)試自動(dòng)化工具,可以使用不同的語(yǔ)言中編寫(xiě)測(cè)試用例的能力。Gauge本質(zhì)上是一個(gè)BDD(行為驅(qū)動(dòng)開(kāi)發(fā))測(cè)試框架。如果你了...
閱讀 2474·2021-11-23 09:51
閱讀 533·2019-08-30 13:59
閱讀 1835·2019-08-29 11:20
閱讀 2542·2019-08-26 13:41
閱讀 3249·2019-08-26 12:16
閱讀 741·2019-08-26 10:59
閱讀 3336·2019-08-26 10:14
閱讀 607·2019-08-23 17:21