python+playwright微軟自動化工具的使用
它支持主流的瀏覽器,包含:Chrome、Firefox、Safari、Microsoft Edge 等,同時支持以無頭模式、有頭模式運行
playwright-python 提供了同步、異步的 API,可以結(jié)合 Pytest 測試框架使用,并且支持瀏覽器端的自動化腳本錄制
項目地址:https://github.com/microsoft/playwright-python
安裝playwright-python,執(zhí)行命令:pip install playwright
安裝成功之后,執(zhí)行命令:python -m playwright install,自動下載 Chromeium、Firefox、Safari(WebKit)瀏覽器驅(qū)動到本地
同步
同步的關(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 基本上囊括常見的自動化操作
異步
異步步的關(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())
事實上,Playwright 是一個跨語言的自動化框架,支持 Python、Java、JS 等
Playwright 相比傳統(tǒng)的自動化框架 Selenium 來說,在 Context 上下文及 API 使用上,顯得更簡潔且強大
到此這篇關(guān)于python+playwright微軟自動化工具的使用的文章就介紹到這了,更多相關(guān)python playwright微軟自動化工具內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Pandas數(shù)據(jù)分析之iloc和loc的用法詳解
Pandas 是一個開放源碼、BSD 許可的庫,提供高性能、易于使用的數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析工具,它是一個強大的分析結(jié)構(gòu)化數(shù)據(jù)的工具集,基礎(chǔ)是 Numpy2021-11-11python:pandas合并csv文件的方法(圖書數(shù)據(jù)集成)
下面小編就為大家分享一篇python:pandas合并csv文件的方法(圖書數(shù)據(jù)集成),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04