Skip to main content

Playwright

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

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)

stop

Added before v1.9 playwright.stop

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

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()

使用方式

playwright.stop()

傳回值


屬性 (Properties)

chromium

Added before v1.9 playwright.chromium

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

使用方式

playwright.chromium

型別 (Type)


devices

Added before v1.9 playwright.devices

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

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)

使用方式

playwright.devices

型別 (Type)


firefox

Added before v1.9 playwright.firefox

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

使用方式

playwright.firefox

型別 (Type)


request

Added in: v1.16 playwright.request

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

使用方式

playwright.request

型別 (Type)


selectors

Added before v1.9 playwright.selectors

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

使用方式

playwright.selectors

型別 (Type)


webkit

Added before v1.9 playwright.webkit

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

使用方式

playwright.webkit

型別 (Type)