Browser Automation with Playwright
Playwright lets you click, type, and screenshot from Python — the standard for headless browser control.
Browser Automation with Playwright is a free AI Agents lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Playwright Is the Standard
For controlling a real browser from Python, Playwright is the de-facto standard. It replaces Selenium for most use cases.
Install
# pip install playwright
playwright install # downloads Chromium, Firefox, WebKitBasic Usage
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto('https://example.com')
print(page.title())
browser.close()Selectors
Playwright supports CSS, XPath, and text selectors:
page.click('button.submit') # CSS
page.click('text=Submit') # text
page.click('role=button[name="Submit"]') # ARIAFilling Forms
page.fill('input[name="email"]', 'alice@example.com')
page.fill('input[name="password"]', 'secret')
page.click('button[type="submit"]')
page.wait_for_url('https://example.com/dashboard')Wait for Elements
Playwright auto-waits for elements to be visible and interactable:
page.click('button.export')
page.wait_for_selector('text=Download Ready')
page.click('text=Download Ready')Reading Content
title = page.title()
body_text = page.inner_text('body')
href = page.get_attribute('a.download', 'href')Screenshots
page.screenshot(path='page.png')
page.locator('.header').screenshot(path='header.png')Full-Page Screenshot
page.screenshot(path='full.png', full_page=True)Network Interception
Block ads/trackers or mock APIs:
def block_ads(route):
if 'analytics' in route.request.url:
route.abort()
else:
route.continue_()
page.route('**/*', block_ads)Async API
from playwright.async_api import async_playwright
import asyncio
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.goto('https://example.com')
await browser.close()
asyncio.run(main())Browser Context = One Session
Use contexts to isolate cookies between agent runs:
context = browser.new_context()
page = context.new_page()
# context.cookies, context.storage_state are isolatedPersistent Storage
Save login state to skip re-authentication:
context.storage_state(path='state.json')
# Later:
context = browser.new_context(storage_state='state.json')Tracing for Debugging
context.tracing.start(screenshots=True, snapshots=True)
# do stuff
context.tracing.stop(path='trace.zip')
# Open with: playwright show-trace trace.zipWhy Playwright?
What makes Playwright better than older browser-automation tools?
Recap
Playwright for browser control: install, goto, click, fill, screenshot. The base layer for any browser agent.
Frequently asked questions
Is the “Browser Automation with Playwright” lesson free?
Yes — the full text of “Browser Automation with Playwright” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.
What will I learn in “Browser Automation with Playwright”?
Playwright lets you click, type, and screenshot from Python — the standard for headless browser control. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Agents?
No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Browser Automation with Playwright” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Agents lesson?
Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Browser Automation with Playwright
- Vision Models for Screen Understanding
- Computer-Use Patterns (Anthropic Computer-Use)
- Building a Reliable Form-Filling Agent