Playwright is a browser automation library maintained by Microsoft. It wraps CDP (see BA-001) into a high level API and adds support for Chromium, Firefox and WebKit with a single interface. It also handles the browser lifecycle for you.
First install it:
pip install pytest-playwright
playwright install
This installs the library and downloads the browser binaries.
Here is the simplest possible script that opens a page and takes a screenshot:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://example.com")
page.screenshot(path="example.png")
browser.close()
If you run this, a file named example.png will appear in your current directory. Playwright launched a headless Chromium, navigated to example.com and took a screenshot.
You can also interact with elements. Here is how to search on Google and extract the results:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://www.google.com")
page.fill("textarea[name=q]", "playwright python")
page.keyboard.press("Enter")
page.wait_for_selector("h3")
titles = page.locator("h3").all_text_contents()
for t in titles[:5]:
print(t)
browser.close()
The headless=False flag makes the browser window visible so you can see what is happening. Remove it for headless mode.
Playwright also handles waiting automatically. When you call page.goto() it waits for the page to load. When you call page.click() it waits for the element to be visible. This removes the need for explicit sleep calls.
To fill a form and submit:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.goto("https://example.com/login")
page.fill("#username", "admin")
page.fill("#password", "secret")
page.click("#submit")
page.wait_for_url("https://example.com/dashboard")
print("Login done")
browser.close()
Each page.wait_for_* method blocks until the condition is met or a timeout is reached. The default timeout is 30 seconds.
You can also intercept network requests:
from playwright.sync_api import sync_playwright
def log_request(request):
print(f"{request.method} {request.url}")
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.on("request", log_request)
page.goto("https://example.com")
browser.close()
This prints every HTTP request the page makes. Useful for debugging or capturing API calls.
Playwright is the easiest way to automate browsers today. It works across browsers and operating systems, and the API is consistent regardless of which browser you choose.
That's all for now.
Thanks for reading!
Top comments (0)