Playwright recently won the hearts of developers and became the most-used library to control headless browsers programmatically.
Its multi-language and multi-browser support makes it the perfect framework for various use cases, from testing to building automation and scraping scripts.
This article digs into its modern API with complete examples showcasing how to get the best of Playwright.
Know your fundamentals
First, install Playwright with the latest tag to always benefit from the last available features:
npm i -S playwright-core@latest
Note: Use the _playwright-core_ package instead of the _playwright_ one to build automation or scraping scripts.
Now, let’s go over some of Playwright’s essential concepts before moving forward with essential tips.
Key concepts: Browser, BrowserContexts, Pages and Locators
Playwright enables you to interact with web pages through 4 main concepts: Browser, BrowserContexts, Pages, and Locators.
The Browser, BrowserContext, and Page objects represent different layers of the underlying Browser. In short, a Browser is identical to a regular Browser process; a BrowserContext is an isolated context with dedicated cookies and cache, while a Page is an actual Browser tab.
Locator enables developers to locate elements on a page in a finer and more stable way than regular CSS Selectors. We will cover how to build stable locators later in this article.
Here is how those 4 pillars concepts are commonly used in code:
Read our Understand Playwright’s BrowserContexts and Pages article to learn how to leverage each of them.
The Auto-waiting mechanism
In addition to the key concepts, Playwright also brings an innovative approach to locators with the “Auto-waiting” functionality.
“Auto-waiting” is a feature embedded in Locator that guarantees that each action is performed on a “ready to interact” element. In this case, calling .click() on a Locator will check that:
- The locator identifies exactly one DOM element.
- The element is visible.
- The element is stable.
- The element receives events.
- The element is enabled.
Below is an example of how that works:
As for Playwright, it removes the need to manually check readiness:
Evaluating JavaScript (cautiously)
Playwright enables you to evaluate JavaScript in the page context:
Running code within the browser is slow — use it only for:
- Reading page-only values (
window, globals) - Running authenticated API calls
- Interacting with Chrome extensions
- Command batching
How to share variables with evaluation blocks
Example that fails:
Corrected using parameter binding:
How to load pages efficiently
Choosing the right loading strategy matters:
Playwright supports four waitUntil options:
"commit"— raw HTML only"domcontentloaded"— DOM ready"load"— all resources loaded"networkidle"— no network activity for 500 ms
Rule of thumb:
Use "domcontentloaded" for extraction and “load” for interaction.
Favor Locators to Selectors
CSS selectors are fragile; Locators are stable.
CSS Selector:
Locator equivalent:
Locator logical operators
Parent selection
CSS pseudo-classes in locators
Better performance with Route API, batching, and parallelization
Batching commands
Filter out unnecessary resources
Parallel navigation
Multiple BrowserContext improve speed — but JS-heavy pages limit scalability.
Using reliable headless browsers
Browserbase provides:
- Enhanced Observability (Session Inspector)
- Stealth Browser (anti-bot, proxies, captcha solving)
- Advanced Capabilities (extensions, downloads, long sessions)
- Flexible Integration
- Robust Infrastructure
Start in under 5 minutes:
https://github.com/browserbase/quickstart-playwright-js