python palywright庫基本使用
開源方:微軟
安裝:pip install playwright;python -m playwright install
特點(diǎn):自動(dòng)化腳本錄制;有同步、異步api
生成代碼指令:python -m playwright codegen其他:需要Python 3.7及以上;官方api為node版本,python版本待補(bǔ)充
同步:關(guān)鍵字為:sync_playwrightfrom time import sleepfrom playwright import sync_playwrightwith 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 asynciofrom playwright import async_playwrightasync 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 測(cè)試
@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_playwrightwith 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ò)請(qǐng)求
from playwright import sync_playwrightwith 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ò)請(qǐng)求
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庫的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. python 編寫輸出到csv的操作2. 利用原生JS實(shí)現(xiàn)歡樂水果機(jī)小游戲3. js的一些潛在規(guī)則使用分析4. Python PyQt5-圖形界面的美化操作5. JAMon(Java Application Monitor)備忘記6. PHP swoole的process模塊創(chuàng)建和使用子進(jìn)程操作示例7. 如何用 Python 制作一個(gè)迷宮游戲8. Python PyQt5中彈出子窗口解決子窗口一閃而過的問題9. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例10. python用moviepy對(duì)視頻進(jìn)行簡(jiǎn)單的處理
