Skip to content

🎭 Playwright for Python PyPI versionJoin Slack

Playwright is a Python library to automate Chromium, Firefox and WebKit browsers with a single API. Playwright delivers automation that is ever-green, capable, reliable and fast. See how Playwright is better.

LinuxmacOSWindows
Chromium 91.0.4469.0
WebKit 14.2
Firefox 89.0b2

Headless execution is supported for all browsers on all platforms.

Usage

pip install playwright
playwright install

This installs Playwright and browser binaries for Chromium, Firefox and WebKit. Playwright requires Python 3.7+.

Record and generate code

Playwright can record user interactions in a browser and generate code. See demo.

# Pass --help to see all options
playwright codegen

Playwright offers both sync (blocking) API and async API. They are identical in terms of capabilities and only differ in how one consumes the API.

Sync API

This is our default API for short snippets and tests. If you are not using asyncio in your application, it is the easiest to use Sync API notation.

fromplaywright.sync_apiimportsync_playwrightwithsync_playwright() asp:
forbrowser_typein [p.chromium, p.firefox, p.webkit]:
browser=browser_type.launch()
page=browser.new_page()
page.goto('http://whatsmyuseragent.org/')
page.screenshot(path=f'example-{browser_type.name}.png')
browser.close()

Async API

If your app is based on the modern asyncio loop and you are used to async/await constructs, Playwright exposes Async API for you. You should use this API inside a Python REPL supporting asyncio like with python -m asyncio

$ python -m asyncio
importasynciofromplaywright.async_apiimportasync_playwrightasyncdefmain():
asyncwithasync_playwright() asp:
forbrowser_typein [p.chromium, p.firefox, p.webkit]:
browser=awaitbrowser_type.launch()
page=awaitbrowser.new_page()
awaitpage.goto('http://whatsmyuseragent.org/')
awaitpage.screenshot(path=f'example-{browser_type.name}.png')
awaitbrowser.close()
asyncio.run(main())

With pytest

Use our pytest plugin for Playwright.

deftest_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.wait_for_selector("text=microsoft/Playwright")

Interactive mode (REPL)

Blocking REPL, as in CLI:

>>>fromplaywright.sync_apiimportsync_playwright>>>playwright=sync_playwright().start()
# Use playwright.chromium, playwright.firefox or playwright.webkit# Pass headless=False to see the browser UI>>>browser=playwright.chromium.launch()
>>>page=browser.new_page()
>>>page.goto("http://whatsmyuseragent.org/")
>>>page.screenshot(path="example.png")
>>>browser.close()
>>>playwright.stop()

Async REPL such as asyncio REPL:

$ python -m asyncio
>>>fromplaywright.async_apiimportasync_playwright>>>playwright=awaitasync_playwright().start()
>>>browser=awaitplaywright.chromium.launch()
>>>page=awaitbrowser.new_page()
>>>awaitpage.goto("http://whatsmyuseragent.org/")
>>>awaitpage.screenshot(path="example.png")
>>>awaitbrowser.close()
>>>awaitplaywright.stop()

Examples

Mobile and geolocation

This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.

fromplaywright.sync_apiimportsync_playwrightwithsync_playwright() asp:
iphone_11=p.devices["iPhone 11 Pro"]
browser=p.webkit.launch(headless=False)
context=browser.new_context(
**iphone_11,
locale="en-US",
geolocation={"longitude": 12.492507, "latitude": 41.889938 },
permissions=["geolocation"]
)
page=context.new_page()
page.goto("https://maps.google.com")
page.click("text=Your location")
page.screenshot(path="colosseum-iphone.png")
browser.close()
Async variant
importasynciofromplaywright.async_apiimportasync_playwrightasyncdefmain():
asyncwithasync_playwright() asp:
iphone_11=p.devices["iPhone 11 Pro"]
browser=awaitp.webkit.launch(headless=False)
context=awaitbrowser.new_context(
**iphone_11,
locale="en-US",
geolocation={"longitude": 12.492507, "latitude": 41.889938},
permissions=["geolocation"]
)
page=awaitcontext.newPage()
awaitpage.goto("https://maps.google.com")
awaitpage.click("text="Yourlocation"")
awaitpage.screenshot(path="colosseum-iphone.png")
awaitbrowser.close()
asyncio.run(main())

Evaluate JS in browser

This code snippet navigates to example.com in Firefox, and executes a script in the page context.

fromplaywright.sync_apiimportsync_playwrightwithsync_playwright() asp:
browser=p.firefox.launch()
page=browser.new_page()
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()
Async variant
importasynciofromplaywright.async_apiimportasync_playwrightasyncdefmain():
asyncwithasync_playwright() asp:
browser=awaitp.firefox.launch()
page=awaitbrowser.new_page()
awaitpage.goto("https://www.example.com/")
dimensions=awaitpage.evaluate("""() => { return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight, deviceScaleFactor: window.devicePixelRatio } }""")
print(dimensions)
awaitbrowser.close()
asyncio.run(main())

Intercept network requests

This code snippet sets up request routing for a Chromium page to log all network requests.

fromplaywright.sync_apiimportsync_playwrightwithsync_playwright() asp:
browser=p.chromium.launch()
page=browser.new_page()
deflog_and_continue_request(route, request):
print(request.url)
route.continue_()
# Log and continue all network requestspage.route("**/*", log_and_continue_request)
page.goto("http://todomvc.com")
browser.close()
Async variant
importasynciofromplaywright.async_apiimportasync_playwrightasyncdefmain():
asyncwithasync_playwright() asp:
browser=awaitp.chromium.launch()
page=awaitbrowser.new_page()
asyncdeflog_and_continue_request(route, request):
print(request.url)
awaitroute.continue_()
# Log and continue all network requestsawaitpage.route("**/*", log_and_continue_request)
awaitpage.goto("http://todomvc.com")
awaitbrowser.close()
asyncio.run(main())

Documentation

Check out our new documentation site!

About

Python version of the Playwright testing and automation library.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages