跳到正文
本页尚未翻译成中文,以下为英文原文。 查看英文版

Playwright ​

Added in: v1.8

Playwright module provides a method to launch a browser instance. The following is a typical example of using Playwright to drive automation:

python
import asyncio
from playwright.async_api import async_playwright, Playwright

async def run(playwright: Playwright):
    chromium = playwright.chromium # or "firefox" or "webkit".
    browser = await chromium.launch()
    page = await browser.new_page()
    await page.goto("http://example.com")
    # other actions...
    await browser.close()

async def main():
    async with async_playwright() as playwright:
        await run(playwright)
asyncio.run(main())
python
from playwright.sync_api import sync_playwright, Playwright

def run(playwright: Playwright):
    chromium = playwright.chromium # or "firefox" or "webkit".
    browser = chromium.launch()
    page = browser.new_page()
    page.goto("http://example.com")
    # other actions...
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

Methods ​

playwright.stop() ​

Added in: v1.8

Terminates this instance of Playwright in case it was created bypassing the Python context manager. This is useful in REPL applications.

python
from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()

browser = playwright.chromium.launch()
page = browser.new_page()
page.goto("https://playwright.dev/")
page.screenshot(path="example.png")
browser.close()

playwright.stop()

Properties ​

playwright.chromium ​

Added in: v1.8

This object can be used to launch or connect to Chromium, returning instances of Browser.

Type

  • BrowserType

playwright.devices ​

Added in: v1.8

Returns a dictionary of devices to be used with browser.new_context() or browser.new_page().

python
import asyncio
from playwright.async_api import async_playwright, Playwright

async def run(playwright: Playwright):
    webkit = playwright.webkit
    iphone = playwright.devices["iPhone 6"]
    browser = await webkit.launch()
    context = await browser.new_context(**iphone)
    page = await context.new_page()
    await page.goto("http://example.com")
    # other actions...
    await browser.close()

async def main():
    async with async_playwright() as playwright:
        await run(playwright)
asyncio.run(main())
python
from playwright.sync_api import sync_playwright, Playwright

def run(playwright: Playwright):
    webkit = playwright.webkit
    iphone = playwright.devices["iPhone 6"]
    browser = webkit.launch()
    context = browser.new_context(**iphone)
    page = context.new_page()
    page.goto("http://example.com")
    # other actions...
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

Type

  • Object

playwright.firefox ​

Added in: v1.8

This object can be used to launch or connect to Firefox, returning instances of Browser.

Type

  • BrowserType

playwright.request ​

Added in: v1.16

Exposes API that can be used for the Web API testing.

Type

  • APIRequest

playwright.selectors ​

Added in: v1.8

Selectors can be used to install custom selector engines. See extensibility for more information.

Type

  • Selectors

playwright.webkit ​

Added in: v1.8

This object can be used to launch or connect to WebKit, returning instances of Browser.

Type

  • BrowserType

基于 Apache 2.0 许可证发布。