Playwright is a Python library to automate Chromium, Firefox and WebKit with a single API. Playwright is built to enable cross-browser web automation that is ever-green, capable, reliable and fast.
| Linux | macOS | Windows | |
|---|---|---|---|
| Chromium 86.0.4217.0 | ✅ | ✅ | ✅ |
| WebKit 14.0 | ✅ | ✅ | ✅ |
| Firefox 78.0b5 | ✅ | ✅ | ✅ |
Headless execution is supported for all the browsers on all platforms.
This is a Python 3 version of the https://github.com/microsoft/playwright project.
pip install playwright This installs Playwright and browser binaries for Chromium, Firefox and WebKit. Once installed, you can import Playwright in a Python script and automate web browser interactions.
Playwright is built to automate the broad and growing set of web browser capabilities used by Single Page Apps and Progressive Web Apps.
- Scenarios that span multiple page, domains and iframes
- Auto-wait for elements to be ready before executing actions (like click, fill)
- Intercept network activity for stubbing and mocking network requests
- Emulate mobile devices, geolocation, permissions
- Support for web components via shadow-piercing selectors
- Native input events for mouse and keyboard
- Upload and download files
This code snippet navigates to whatsmyuseragent.org in Chromium, Firefox and WebKit, and saves 3 screenshots.
from playwright import sync_playwright with sync_playwright() as p: for browser_type in [p.chromium, p.firefox, p.webkit]: browser = browser_type.launch() page = browser.newPage() page.goto('http://whatsmyuseragent.org/') page.screenshot(path=f'example-{browser_type.name}.png') browser.close()This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.
from playwright import sync_playwright with sync_playwright() as p: iphone_11 = p.devices['iPhone 11 Pro'] browser = p.webkit.launch(headless=False) context = browser.newContext( **iphone_11, locale='en-US', geolocation={ 'longitude': 12.492507, 'latitude': 41.889938 }, permissions=['geolocation'] ) page = context.newPage() page.goto('https://maps.google.com') page.click('text="Your location"') page.waitForRequest('*preview/pwa') page.screenshot(path='colosseum-iphone.png') browser.close()... or, if you are comfortable using asyncio, you can do the following:
import asyncio from playwright import async_playwright async def main(): async with async_playwright() as p: iphone_11 = p.devices['iPhone 11 Pro'] browser = await p.webkit.launch(headless=False) context = await browser.newContext( **iphone_11, locale='en-US', geolocation={ 'longitude': 12.492507, 'latitude': 41.889938 }, permissions=['geolocation'] ) page = await context.newPage() await page.goto('https://maps.google.com') await page.click('text="Your location"') await page.waitForRequest('*preview/pwa') await page.screenshot(path='colosseum-iphone.png') await browser.close() asyncio.get_event_loop().run_until_complete(main())This code snippet navigates to example.com in Firefox, and executes a script in the page context.
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()... and again, async version:
import asyncio from playwright import async_playwright async def main(): async with async_playwright() as p: browser = await p.firefox.launch() page = await browser.newPage() await page.goto('https://www.example.com/') dimensions = await page.evaluate('''() => { return { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight, deviceScaleFactor: window.devicePixelRatio } }''') print(dimensions) await browser.close() asyncio.get_event_loop().run_until_complete(main())This code snippet sets up request routing for a Chromium page to log all network requests.
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_() # Log and continue all network requests page.route('**', lambda route, request: log_and_continue_request(route, request)) page.goto('http://todomvc.com') browser.close()... async version:
import asyncio from playwright import async_playwright async def main(): async with async_playwright() as p: browser = await p.chromium.launch() page = await browser.newPage() def log_and_continue_request(route, request): print(request.url) asyncio.create_task(route.continue_()) # Log and continue all network requests await page.route('**', lambda route, request: log_and_continue_request(route, request)) await page.goto('http://todomvc.com') await browser.close() asyncio.get_event_loop().run_until_complete(main())We are ready for your feedback, but we are still covering Playwright Python with the tests, so expect a bumpy ride and don't use for production.