您好,登錄后才能下訂單哦!
palywright庫怎么在python中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
開源方:微軟
安裝:pip install playwright;python -m playwright install
特點:自動化腳本錄制;有同步、異步api
生成代碼指令:python -m playwright codegen
其他:需要Python 3.7及以上;官方api為node版本,python版本待補充
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) # 默認無頭,這樣為有頭模式 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()
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.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")
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()
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_()
page.route('**', lambda route, request: log_and_continue_request(route, request)) page.goto('http://todomvc.com') browser.close()
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。