1. 前言最近,微軟開源了一款非常強大的 Python 自動化依賴庫:playwright-python 它支持主流的瀏覽器,包含:Chrome、Firefox、Safari、Microsoft Edge 等,同時支持以無頭模式、有頭模式運行 playwright-python?提供了同步、異步的 API,可以結(jié)合 Pytest?測試框架使用,并且支持瀏覽器端的自動化腳本錄制 項目地址: https://github.com/microsoft/playwright-python 2. 準備在實戰(zhàn)之前,我們只需要 2 步 第 1 步,安裝 playwright-python 依賴庫? #?安裝依賴庫 pip3?install?playwright 第 2 步,安裝主流的瀏覽器驅(qū)動 這樣,會將?Chromeium、Firefox、Webkit 瀏覽器驅(qū)動下載到本地 #?安裝瀏覽器驅(qū)動 python?-m?playwright?install 3. 實戰(zhàn)一下3-1 錄制腳本 我們先查看錄制腳本的命令說明
其中 比如#?我們通過下面命令打開?Chrome?瀏覽器開始錄制腳本 #?指定生成語言為:Python(默認Python,可選) #?保存的文件名:1.py(可選) #?瀏覽器驅(qū)動:webkit(默認webkit,可選) #?最后跟著要打開的目標網(wǎng)站(默認僅僅是打開瀏覽器,可選) python?-m?playwright?codegen?--target?python?-o?'1.py'?-b?webkit?https://www.baidu.com 接著,在瀏覽器模擬搜索一次的操作,然后關(guān)閉瀏覽器 最后,自動化腳本會自動生成,保存到文件中 from?playwright?import?sync_playwright
def?run(playwright): ????browser?=?playwright.webkit.launch(headless=False) ????context?=?browser.newContext()
????#?Open?new?page ????page?=?context.newPage()
????#?Go?to?https://www.baidu.com/ ????page.goto("https://www.baidu.com/")
????#?Fill?input[name="wd"] ????page.fill("input[name=\"wd\"]",?"AirPython")
????#?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=AirPython&fenlei=256&rsv_pq=a1739d870005eec3&rsv_t=e640wwS33ra1Koivxvy1WyTxyknRwnllWiw4JBqIYd/KUN/WKpWLtL2b2+0&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=21&rsv_sug1=18&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=6199&rsv_sug4=6199"): ????with?page.expect_navigation(): ????????page.press("input[name=\"wd\"]",?"Enter")
????#?Close?page ????page.close()
????#?--------------------- ????context.close() ????browser.close()
with?sync_playwright()?as?playwright: ????run(playwright)
3-2 同步
同步的關(guān)鍵字為:sync_playwright 比如,我們依次使用三個瀏覽器內(nèi)核打開瀏覽器,然后百度一下,接著對在搜索界面截圖,最后關(guān)閉瀏覽器 from?time?import?sleep
from?playwright?import?sync_playwright
#?注意:默認是無頭模式
with?sync_playwright()?as?p: ????#?分別對應(yīng)三個瀏覽器驅(qū)動 ????for?browser_type?in?[p.chromium,?p.firefox,?p.webkit]:
????????#?指定為有頭模式,方便查看 ????????browser?=?browser_type.launch(headless=False) ????????page?=?browser.newPage() ????????page.goto('http://baidu.com')
????????#?執(zhí)行一次搜索操作 ????????page.fill("input[name=\"wd\"]",?"AirPython") ????????with?page.expect_navigation(): ????????????page.press("input[name=\"wd\"]",?"Enter")
????????#?等待頁面加載完全 ????????page.waitForSelector("text=百度熱榜")
????????#?截圖 ????????page.screenshot(path=f'example-{browser_type.name}.png')
????????#?休眠5s ????????sleep(5)
????????#?關(guān)閉瀏覽器 ????????browser.close()
需要指出的是,playwright-python 內(nèi)置的 API?基本上囊括常見的自動化操作 3-3?異步
異步步的關(guān)鍵字為:async_playwright 結(jié)合?asyncio,我們同時執(zhí)行上面的操作 import?asyncio
from?playwright?import?async_playwright
#?異步執(zhí)行 async?def?main(): ????async?with?async_playwright()?as?p: ????????for?browser_type?in?[p.chromium,?p.firefox,?p.webkit]: ????????????#?指定為有頭模式,方便查看 ????????????browser?=?await?browser_type.launch(headless=False) ????????????page?=?await?browser.newPage()
????????????await?page.goto('http://baidu.com')
????????????#?執(zhí)行一次搜索操作 ????????????await?page.fill("input[name=\"wd\"]",?"AirPython") ????????????await?page.press("input[name=\"wd\"]",?"Enter")
????????????#?等待頁面加載完全 ????????????await?page.waitForSelector("text=百度熱榜")
????????????#?截圖 ????????????await?page.screenshot(path=f'example-{browser_type.name}.png')
????????????await?browser.close()
asyncio.get_event_loop().run_until_complete(main()) 4. 最后事實上,Playwright 是一個跨語言的自動化框架,支持 Python、Java、JS 等
|