Master Playwright-Python: Quick Guide to Browser Automation for Beginners

This article introduces Microsoft’s open‑source Playwright‑Python library, explains its advantages over Selenium, walks through installation, code generation, synchronous and asynchronous usage, mobile emulation, and highlights its strengths and current limitations for Python developers.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Playwright-Python: Quick Guide to Browser Automation for Beginners

1. Introduction to Playwright‑Python

Playwright‑Python is an open‑source automation library from Microsoft that lets Python developers control Chromium, Firefox, and WebKit browsers through a single API. It simplifies web automation for beginners, offering a more approachable alternative to Selenium.

2. Installation

pip install playwright
python -m playwright install

Requirements: Python 3.7+ and browser driver files for Chromium, Firefox, and WebKit.

3. Recording Actions (codegen)

Playwright can record user interactions in a browser and automatically generate Python code.

python -m playwright codegen https://example.com

Common options:

-o <file>: save the generated script

--target python|javascript: choose output language (default python)

-b <browser>: specify browser driver

Example of saving a script:

python -m playwright codegen --target python -o 'my.py' -b chromium https://www.baidu.com
Playwright codegen UI
Playwright codegen UI

4. Generated Script Example

from playwright import sync_playwright

def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://www.baidu.com/")
    page.click("input[name=\"wd\"]")
    page.fill("input[name=\"wd\"]", "jingdong")
    page.click("text=京东")
    # ... additional actions ...
    context.close()
    browser.close()

with sync_playwright() as p:
    run(p)

5. Synchronous & Asynchronous APIs

Synchronous example (multiple browsers)

from playwright import sync_playwright
with sync_playwright() as p:
    for browser_type in [p.chromium, p.firefox, p.webkit]:
        browser = browser_type.launch()
        page = browser.new_page()
        page.goto('https://baidu.com/')
        page.screenshot(path=f'example-{browser_type.name}.png')
        browser.close()

Asynchronous example (using asyncio)

import asyncio
from playwright import async_playwright

async def main():
    async with async_playwright() as p:
        for browser_type in [p.chromium, p.firefox, p.webkit]:
            browser = await browser_type.launch()
            page = await browser.new_page()
            await page.goto('http://baidu.com/')
            await page.screenshot(path=f'example-{browser_type.name}.png')
            await browser.close()

asyncio.run(main())

6. Mobile Emulation

Playwright can simulate mobile devices such as iPhone 11 Pro with Safari.

from playwright import sync_playwright
with sync_playwright() as p:
    iphone_11 = p.devices['iPhone 11 Pro']
    browser = p.webkit.launch(headless=False)
    context = browser.new_context(**iphone_11, locale='en-US',
        geolocation={'longitude': 12.492507, 'latitude': 41.889938},
        permissions=['geolocation'])
    page = context.new_page()
    page.goto('https://maps.google.com')
    page.click('text="Your location"')
    page.screenshot(path='colosseum-iphone.png')
    browser.close()
Mobile emulation example
Mobile emulation example

7. Advantages

Supports Chromium, Firefox, WebKit across Windows, macOS, Linux.

Headless and headed modes for CI and debugging.

Automatic waiting for elements, reducing flaky tests.

Parallel browser contexts for efficient resource use.

Resilient selectors based on text or accessibility attributes.

Handles multiple pages, frames, network interception, file upload/download, and modern web APIs.

8. Limitations

No support for legacy Edge or IE 11.

Java and Ruby bindings are not yet available.

Mobile testing relies on device emulation, not real devices.

9. Current Status

Playwright‑Python is at version 1.7.0 and continues to evolve, offering a robust, beginner‑friendly solution for web automation.

Documentation: https://microsoft.github.io/playwright-python/index.html GitHub: https://github.com/microsoft/playwright-python
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythontestingWeb ScrapingBrowser AutomationPlaywrightMobile Emulation
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.