Playwright-Python

Language: Python

Web Automation

Playwright was developed by Microsoft to provide a reliable and fast automation library for testing modern web applications. The Python bindings allow developers to use Playwright's cross-browser capabilities, including headless and headful modes, to automate interactions with web pages.

Playwright-Python is a Python library for automating web browsers. It enables end-to-end testing, web scraping, and browser automation across Chromium, Firefox, and WebKit with a single API.

Installation

pip: pip install playwright playwright install
conda: conda install -c conda-forge playwright

Usage

Playwright allows you to launch browsers, navigate pages, interact with elements, capture screenshots, and evaluate JavaScript code. It supports synchronous and asynchronous APIs and provides robust mechanisms for waiting, network interception, and handling popups or frames.

Opening a page and taking a screenshot

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')
    page.screenshot(path='example.png')
    browser.close()

Launches a Chromium browser in headless mode, navigates to a URL, takes a screenshot, and closes the browser.

Clicking a button

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.click('text=More information')
    browser.close()

Navigates to a page and clicks a button or link with the text 'More information'.

Filling a form and submitting

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', 'myuser')
    page.fill('#password', 'mypassword')
    page.click('#login')
    browser.close()

Fills a login form using selectors and submits it.

Waiting for elements

page.goto('https://example.com')
page.wait_for_selector('#content')

Waits until the specified element appears in the DOM before continuing.

Handling popups

page.on('dialog', lambda dialog: dialog.accept())
page.click('#open-popup')

Listens for dialog popups and automatically accepts them.

Extracting page content

content = page.inner_text('#main')
print(content)

Extracts the text content of a specific element on the page.

Error Handling

TimeoutError: Increase the timeout or ensure the element is correctly targeted and visible before interaction.
PlaywrightError: No node found for selector: Verify the selector is correct and matches an element on the page.
BrowserError: Ensure the browser binaries are installed with `playwright install` and the environment supports GUI if headless=False.

Best Practices

Use synchronous API for simple scripts and asynchronous API for large-scale automation.

Prefer headless mode for faster execution unless debugging.

Leverage selectors efficiently to avoid brittle scripts.

Wait explicitly for elements or network events to ensure reliability.

Organize automation scripts into reusable functions or classes for maintainability.