| id | playwright |
|---|---|
| title | Browser automation with Playwright |
| description | Build an Apify Actor that scrapes dynamic web pages using Playwright browser automation. |
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import CodeBlock from '@theme/CodeBlock'; import RunnableCodeBlock from '@site/src/components/RunnableCodeBlock';
import PlaywrightExample from '!!raw-loader!roa-loader!./code/03_playwright.py';
In this guide, you'll learn how to use Playwright for browser automation and web scraping in your Apify Actors.
Playwright is a tool for web automation and testing that can also be used for web scraping. It allows you to control a web browser programmatically and interact with web pages just as a human would.
Some of the key features of Playwright for web scraping include:
- Cross-browser support - Playwright supports Chromium, Firefox, and WebKit with a single API, ensuring consistent behavior across all browsers.
- Auto-waiting - Playwright automatically waits for elements to be ready before performing actions, reducing flaky scripts and eliminating the need for manual sleep calls.
- Headless and headful modes - Playwright can run with or without a visible browser window, making it suitable for both local development and containerized environments.
- Powerful selectors - Playwright provides CSS selectors, XPath, text matching, and its own resilient locator API for targeting elements on a page.
- Network interception - Playwright can intercept and modify network requests, allowing you to block unnecessary resources or mock API responses during scraping.
To create Actors which use Playwright, start from the Playwright & Python Actor template.
On the Apify platform, the Actor will already have Playwright and the necessary browsers preinstalled in its Docker image, including the tools and setup necessary to run browsers in headful mode.
When running the Actor locally, you'll need to finish the Playwright setup yourself before you can run the Actor.
{ `source .venv/bin/activate playwright install --with-deps` } { `.venv\\Scripts\\activate playwright install --with-deps` }This is a simple Actor that recursively scrapes data from linked pages on the same site, up to a maximum depth, starting from URLs in the Actor input.
It uses Playwright to open the pages in an automated Chrome browser, and to extract the title, headings, and links after the pages load.
{PlaywrightExample}Running on the Apify platform gives your scraper access to Apify Proxy, which rotates IP addresses to avoid rate limiting and blocking. The example creates a proxy configuration with Actor.create_proxy_configuration and launches the browser through it. Playwright applies the proxy at the browser level, so the whole run shares a single proxy URL rather than rotating per request; the to_playwright_proxy helper splits that URL into the server, username, and password fields Playwright expects. To select specific proxy groups or a country, pass the relevant arguments to Actor.create_proxy_configuration. For more details, see the Proxy management guide.
In this guide you learned how to create Actors that use Playwright to scrape websites. Playwright is a powerful tool that can be used to manage browser instances and scrape websites that require JavaScript execution. See the Actor templates to get started with your own scraping tasks. If you have questions or need assistance, feel free to reach out on our GitHub or join our Discord community. Happy scraping!