python palywright庫基本使用
開源方:微軟
安裝:pip install playwright;python -m playwright install
特點(diǎn):自動化腳本錄制;有同步、異步api
生成代碼指令:python -m playwright codegen
其他:需要Python 3.7及以上;官方api為node版本,python版本待補(bǔ)充
同步:關(guān)鍵字為:sync_playwright
from time import sleep
from playwright import sync_playwright
with sync_playwright() as p:
for browser_type in [p.chromium, p.firefox, p.webkit]:
browser = browser_type.launch(headless=False) # 默認(rèn)無頭,這樣為有頭模式
page = browser.newPage()
page.goto('http://baidu.com')
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')
sleep(5)
browser.close()
異步:關(guān)鍵字為:async_playwright
import asyncio
from playwright import async_playwright
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')
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())
集成 pytest 測試
@pytest.fixture(scope="session")
def test_playwright_is_visible_on_google(page):
page.goto("https://www.google.com")
page.type("input[name=q]", "Playwright GitHub")
page.click("input[type=submit]")
page.waitForSelector("text=microsoft/Playwright")
執(zhí)行 JS 代碼
from playwright import sync_playwright
with sync_playwright() as p:
browser = p.firefox.launch()
page = browser.newPage()
page.goto('https://www.example.com/')
dimensions = page.evaluate('''() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
} }''')
print(dimensions)
browser.close()
中斷網(wǎng)絡(luò)請求
from playwright import sync_playwright with sync_playwright() as p: browser = p.chromium.launch() page = browser.newPage() def log_and_continue_request(route, request): print(request.url) route.continue_()
記錄并繼續(xù)所有網(wǎng)絡(luò)請求
page.route('**', lambda route, request: log_and_continue_request(route, request))
page.goto('http://todomvc.com')
browser.close()
以上就是python palywright庫基本使用的詳細(xì)內(nèi)容,更多關(guān)于python palywright庫的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python+unittest+requests+excel實(shí)現(xiàn)接口自動化測試框架
- Python接口自動化測試框架運(yùn)行原理及流程
- python+appium+yaml移動端自動化測試框架實(shí)現(xiàn)詳解
- Python+unittest+requests 接口自動化測試框架搭建教程
- Python實(shí)現(xiàn)http接口自動化測試的示例代碼
- python自動化測試三部曲之request+django實(shí)現(xiàn)接口測試
- Python3+RIDE+RobotFramework自動化測試框架搭建過程詳解
- jenkins+python自動化測試持續(xù)集成教程
- python利用Excel讀取和存儲測試數(shù)據(jù)完成接口自動化教程
- 使用python+poco+夜神模擬器進(jìn)行自動化測試實(shí)例
- Python+Appium實(shí)現(xiàn)自動化測試的使用步驟
- python selenium自動化測試框架搭建的方法步驟
相關(guān)文章
如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具
這篇文章主要介紹了如何基于Python代碼實(shí)現(xiàn)高精度免費(fèi)OCR工具,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
關(guān)于Python3 lambda函數(shù)的深入淺出
今天小編就為大家分享一篇關(guān)于Python3 lambda函數(shù)的深入淺出,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11
利用Python腳本寫端口掃描器socket,python-nmap
這篇文章主要介紹了利用Python腳本寫端口掃描器socket,python-nmap,文章圍繞主題展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07
提升?Python?代碼運(yùn)行速度的6個(gè)技巧
本文分享了提升?Python?代碼運(yùn)行速度的6個(gè)技巧,Python?比我們想象的運(yùn)行的要快。我們之所以有先入為主的認(rèn)為Python運(yùn)行慢,可能是我們平常的誤用和缺乏使用技巧知識。接下來讓我們看看如何用一些簡單的Trick來提高我們程序的運(yùn)行性能,需要的朋友可以參考一下2022-01-01

