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

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

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.

>>> from playwright.sync_api import sync_playwright

>>> playwright = sync_playwright().start()

>>> browser = playwright.chromium.launch()
>>> page = browser.new_page()
>>> page.goto("http://whatsmyuseragent.org/")
>>> page.screenshot(path="example.png")
>>> browser.close()

>>> playwright.stop()

playwright.chromium

Added in: v1.8

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

playwright.devices

Added in: v1.8

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

from playwright.sync_api import sync_playwright

def run(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.firefox

Added in: v1.8

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

playwright.request

Added in: v1.16

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

playwright.selectors

Added in: v1.8

Selectors can be used to install custom selector engines. See Working with selectors for more information.

playwright.webkit

Added in: v1.8

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