Convert Figma logo to code with AI

microsoft logoplaywright-python

Python version of the Playwright testing and automation library.

11,687
888
11,687
34

Top Related Projects

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

30,518

A browser automation framework and ecosystem.

88,205

JavaScript API for Chrome and Firefox

46,847

Fast, easy and reliable testing for anything that runs in a browser.

18,791

Cross-platform automation framework for all kinds of apps, built on top of the W3C WebDriver protocol

Quick Overview

Playwright-Python is a Python library for browser automation and testing. It provides a high-level API to control Chromium, Firefox, and WebKit browsers programmatically, enabling developers to write robust end-to-end tests and automate web interactions across multiple browser engines.

Pros

  • Cross-browser support for Chromium, Firefox, and WebKit
  • Powerful automation capabilities, including network interception and mocking
  • Fast and reliable execution with auto-wait functionality
  • Supports both synchronous and asynchronous programming models

Cons

  • Steeper learning curve compared to some other testing frameworks
  • Requires separate browser installations, which can increase setup complexity
  • Limited support for older browser versions
  • May require more system resources due to full browser automation

Code Examples

  1. Basic page navigation and screenshot capture:
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()
  1. Interacting with page elements:
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.firefox.launch()
    page = browser.new_page()
    page.goto("https://example.com")
    page.fill("input[name='search']", "Playwright")
    page.click("button[type='submit']")
    page.wait_for_selector(".results")
    results = page.inner_text(".results")
    print(results)
    browser.close()
  1. Handling network requests:
from playwright.sync_api import sync_playwright, Route

def handle_route(route: Route):
    if route.request.resource_type == "image":
        route.abort()
    else:
        route.continue_()

with sync_playwright() as p:
    browser = p.webkit.launch()
    page = browser.new_page()
    page.route("**/*", handle_route)
    page.goto("https://example.com")
    browser.close()

Getting Started

To get started with Playwright-Python:

  1. Install Playwright-Python:

    pip install playwright
    
  2. Install browser binaries:

    playwright install
    
  3. Create a Python script (e.g., test_example.py):

    from playwright.sync_api import sync_playwright
    
    def test_example():
        with sync_playwright() as p:
            browser = p.chromium.launch()
            page = browser.new_page()
            page.goto("https://playwright.dev")
            assert "Playwright" in page.title()
            browser.close()
    
    if __name__ == "__main__":
        test_example()
    
  4. Run the script:

    python test_example.py
    

Competitor Comparisons

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

Pros of Playwright

  • Supports multiple programming languages (JavaScript, TypeScript, Python, .NET, Java)
  • More comprehensive documentation and examples
  • Larger community and ecosystem

Cons of Playwright

  • Steeper learning curve due to broader feature set
  • Potentially more complex setup for specific language bindings

Code Comparison

Playwright (JavaScript):

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await browser.close();
})();

Playwright-python:

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")
    browser.close()

Summary

Playwright is a more comprehensive solution supporting multiple programming languages, while Playwright-python focuses specifically on Python integration. Playwright offers broader documentation and community support, but may have a steeper learning curve. The code structure is similar between the two, with minor syntax differences based on the programming language used.

30,518

A browser automation framework and ecosystem.

Pros of Selenium

  • Wider browser support, including older versions
  • Larger community and ecosystem of tools/plugins
  • More extensive documentation and learning resources

Cons of Selenium

  • Slower execution compared to Playwright
  • Less robust handling of modern web technologies (e.g., shadow DOM)
  • More complex setup and configuration

Code Comparison

Selenium:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://example.com")
element = driver.find_element(By.ID, "my-element")
element.click()

Playwright:

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("#my-element")

Playwright offers a more concise and straightforward API, with built-in auto-waiting and better handling of modern web elements. Selenium requires more explicit waits and locator strategies but provides finer control over browser interactions.

Both tools are powerful for web automation, but Playwright is gaining popularity due to its speed and ease of use, while Selenium remains the industry standard with broader compatibility and community support.

88,205

JavaScript API for Chrome and Firefox

Pros of Puppeteer

  • More mature and established project with a larger community
  • Extensive documentation and resources available
  • Native support for Chrome DevTools Protocol

Cons of Puppeteer

  • Limited to Chromium-based browsers
  • Slower development cycle compared to Playwright
  • Less cross-browser compatibility

Code Comparison

Puppeteer:

const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({path: 'screenshot.png'});
await browser.close();

Playwright-Python:

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()
    page.goto("https://example.com")
    page.screenshot(path="screenshot.png")
    browser.close()

Both Puppeteer and Playwright-Python offer similar functionality for browser automation, but Playwright-Python provides a more unified API for multiple browser engines. Playwright-Python also offers better cross-browser support, including Chromium, Firefox, and WebKit. However, Puppeteer has a larger ecosystem and more extensive documentation due to its longer presence in the market. The code examples demonstrate the similarity in basic operations, with slight differences in syntax and structure between JavaScript and Python implementations.

46,847

Fast, easy and reliable testing for anything that runs in a browser.

Pros of Cypress

  • Built-in automatic waiting and retry mechanisms, reducing flaky tests
  • Extensive built-in debugging tools and time-travel capabilities
  • Strong community support and extensive documentation

Cons of Cypress

  • Limited to testing web applications in Chrome-based browsers
  • Cannot interact with multiple browser tabs or domains in a single test
  • Lacks native mobile testing support

Code Comparison

Cypress:

describe('Login', () => {
  it('should log in successfully', () => {
    cy.visit('/login')
    cy.get('#username').type('user@example.com')
    cy.get('#password').type('password123')
    cy.get('button[type="submit"]').click()
    cy.url().should('include', '/dashboard')
  })
})

Playwright:

def test_login(page):
    page.goto("/login")
    page.fill("#username", "user@example.com")
    page.fill("#password", "password123")
    page.click('button[type="submit"]')
    assert "/dashboard" in page.url

Both frameworks offer concise and readable syntax for writing tests. Cypress uses a chainable API with built-in assertions, while Playwright uses a more traditional approach with explicit assertions. Playwright's Python implementation allows for integration with popular Python testing frameworks like pytest.

18,791

Cross-platform automation framework for all kinds of apps, built on top of the W3C WebDriver protocol

Pros of Appium

  • Supports a wider range of mobile platforms, including iOS, Android, and Windows
  • Allows testing of native, hybrid, and mobile web applications
  • Uses standard WebDriver protocol, making it easier for those familiar with Selenium

Cons of Appium

  • Slower test execution compared to Playwright
  • More complex setup and configuration process
  • Less robust handling of modern web technologies and single-page applications

Code Comparison

Appium (Python):

from appium import webdriver

desired_caps = {
    'platformName': 'Android',
    'deviceName': 'Android Emulator',
    'app': '/path/to/app.apk'
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.find_element_by_id('button_id').click()

Playwright (Python):

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('#button_id')

While Appium excels in mobile testing across multiple platforms, Playwright offers a more streamlined experience for web automation with better performance and easier setup for modern web applications. The code examples demonstrate the different approaches, with Appium focusing on mobile-specific capabilities and Playwright providing a more concise syntax for web interactions.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

🎭 Playwright for Python PyPI version Anaconda version Join Discord

Playwright is a Python library to automate Chromium, Firefox and WebKit browsers with a single API. Playwright delivers automation that is ever-green, capable, reliable and fast. See how Playwright is better.

LinuxmacOSWindows
Chromium 129.0.6668.29✅✅✅
WebKit 18.0✅✅✅
Firefox 130.0✅✅✅

Documentation

https://playwright.dev/python/docs/intro

API Reference

https://playwright.dev/python/docs/api/class-playwright

Example

from playwright.sync_api 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('http://playwright.dev')
        page.screenshot(path=f'example-{browser_type.name}.png')
        browser.close()
import asyncio
from playwright.async_api 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://playwright.dev')
            await page.screenshot(path=f'example-{browser_type.name}.png')
            await browser.close()

asyncio.run(main())

Other languages

More comfortable in another programming language? Playwright is also available in