python palywright庫(kù)基本使用
開源方:微軟
安裝: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)無(wú)頭,這樣為有頭模式 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庫(kù)基本使用的詳細(xì)內(nèi)容,更多關(guān)于python palywright庫(kù)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享2. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法3. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法4. xml中的空格之完全解說(shuō)5. WMLScript的語(yǔ)法基礎(chǔ)6. 匹配模式 - XSL教程 - 47. XML入門的常見問題(四)8. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問題……9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯(cuò)誤頁(yè)的問題
