π Playwright for Python
Playwright is a powerful 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.
- Cross-browser: Automate Chromium, Firefox, and WebKit with the same API
- Cross-platform: Works on Windows, macOS, and Linux
- Cross-language: Same API available in Python, JavaScript, .NET, and Java
- Auto-waiting: Automatically waits for elements to be ready
- Mobile emulation: Test responsive web apps with mobile device simulation
- Network interception: Capture and modify network requests
- Reliable selectors: Built-in support for text, CSS, XPath, and React selectors
# Install Playwright
pip install playwright
# Install browser binaries (Chrome, Firefox, WebKit)
playwright installCreate a file example.py:
from playwright.sync_api import sync_playwright
def main():
with sync_playwright() as p:
# Launch browser
browser = p.chromium.launch(headless=False)
page = browser.new_page()
page.goto('https://playwright.dev')
page.screenshot(path='example.png')
print(f"Page title: {page.title()}")
browser.close()
if __name__ == "__main__":
main()Run your script:
python example.pyfrom 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')
print(f"Screenshot saved: 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')
print(f"Screenshot saved: example-{browser_type.name}.png")
await browser.close()
asyncio.run(main())- Python 3.7 or higher
- pip package manager
- Node.js 14 or higher
-
Create virtual environment (recommended):
python -m venv playwright-env source playwright-env/bin/activate -
Install Playwright:
pip install playwright
-
Install browsers:
playwright install
-
Specific browser:
playwright install chromium playwright install firefox
-
With conda:
conda install -c conda-forge playwright
| Browser | Linux | macOS | Windows |
|---|---|---|---|
| Chromium | β | β | β |
| WebKit | β | β | β |
| Firefox | β | β | β |
"playwright: command not found"
- Use:
python -m playwrightinstead ofplaywright
Browser installation fails
- Check internet connection
- Try:
playwright install --force
Permission errors
- Use:
sudo playwright install
We welcome contributions from the community! Whether you're fixing bugs, adding new features, or improving documentation, your help is appreciated.
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
- Follow the existing code style
- Add tests for new functionality
- Update documentation for changes
- Ensure all tests pass
- Be respectful and constructive in discussions
- π Read our Contributing Guide
- π¬ Join the Discord Community
- π Report issues on GitHub Issues
Happy testing! π