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.
pip install playwright
playwright installconda install -c conda-forge playwrightPlaywright 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.
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.
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'.
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.
page.goto('https://example.com')
page.wait_for_selector('#content')Waits until the specified element appears in the DOM before continuing.
page.on('dialog', lambda dialog: dialog.accept())
page.click('#open-popup')Listens for dialog popups and automatically accepts them.
content = page.inner_text('#main')
print(content)Extracts the text content of a specific element on the page.
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.