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 85.0.4182.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.
import asyncio
from playwright import chromium, firefox, webkit
async def run():
for browser_type in [chromium, firefox, webkit]:
browser = await browser_type.launch()
page = await browser.newPage()
await page.goto('http://whatsmyuseragent.org/')
await page.screenshot(path=f'example-{browser_type.name}.png')
await browser.close()
asyncio.get_event_loop().run_until_complete(run())
This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.
import asyncio
from playwright import webkit, devices
iphone_11 = devices['iPhone 11 Pro']
print(iphone_11)
async def run():
browser = await 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(run())
This code snippet navigates to example.com in Firefox, and executes a script in the page context.
import asyncio
from playwright import firefox
async def run():
browser = await 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(run())
This code snippet sets up request routing for a Chromium page to log all network requests.
import asyncio
from playwright import chromium
async def run():
browser = await chromium.launch()
page = await browser.newPage()
async def log_and_continue_request(route, request):
print(request.url)
await route.continue_()
# Log and continue all network requests
await page.route('**', lambda route, request: asyncio.ensure_future(log_and_continue_request(route, request)))
await page.goto('http://todomvc.com')
await browser.close()
asyncio.get_event_loop().run_until_complete(run())
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.