Playwright
Playwright code examples divided by programming language (JavaScript and Python) for using Residential Proxy with browser automation, sticky sessions, and parallel contexts.
Practical Playwright examples for integrating Residential Proxy into browser automation and web scraping projects, separated into JavaScript (Node.js) and Python sections.
JavaScript (Node.js)
Prerequisites
npm install playwright
npx playwright install chromiumplaywright: Node.js library for browser automation supporting Chromium, Firefox, and WebKit with native proxy authentication.
Proxy Format
Playwright accepts proxy settings via the proxy launch option in JavaScript:
const proxy = {
server: 'http://proxy.mrscraper.com:10000',
username: 'user-country-us-sessid-session1',
password: 'pass123'
};1. Basic Proxy Setup
Demonstrates launching Headless Chromium with authenticated proxy credentials in JavaScript, inspecting the resulting external proxy IP.
import { chromium } from 'playwright';
async function testProxy() {
// Launch browser with proxy settings
const browser = await chromium.launch({
headless: true,
proxy: {
server: 'http://proxy.mrscraper.com:10000',
username: 'user-country-us',
password: 'pass123'
}
});
const page = await browser.newPage();
// Navigate to IP inspection endpoint
await page.goto('https://api.ipify.org?format=json');
const content = await page.textContent('body');
console.log('Your Proxy IP:', content);
await browser.close();
}
testProxy().catch(console.error);Use Case: Fundamental proxy connectivity check and baseline browser launch in Node.js.
2. Multi-Context Sticky Sessions
Shows how to create isolated browser contexts on a single browser instance. Each context maintains its own consistent IP address using unique session IDs (sessid).
import { chromium } from 'playwright';
async function runParallelSessions() {
const browser = await chromium.launch({ headless: true });
const workerSessions = ['worker1', 'worker2', 'worker3'];
// Create isolated context per session
for (const session of workerSessions) {
const context = await browser.newContext({
proxy: {
server: 'http://proxy.mrscraper.com:10000',
username: `user-country-us-sessid-${session}`,
password: 'pass123'
}
});
const page = await context.newPage();
await page.goto('https://api.ipify.org?format=json');
const ipData = await page.textContent('body');
console.log(`Session [${session}]:`, ipData);
await context.close();
}
await browser.close();
}
runParallelSessions().catch(console.error);Use Case: Parallel web scraping where multiple workers maintain independent sticky sessions on a single browser process.
3. Geotargeting & Custom Session Duration
Demonstrates routing Playwright traffic through a German proxy with a 30-minute static session duration (sesstime-30), configuring regional locale settings.
import { chromium } from 'playwright';
async function geotargetedSession() {
const browser = await chromium.launch({
headless: true,
proxy: {
server: 'http://proxy.mrscraper.com:10000',
// German IP with 30-minute static duration
username: 'user-country-de-sessid-shop1-sesstime-30',
password: 'pass123'
}
});
const context = await browser.newContext({
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
locale: 'de-DE'
});
const page = await context.newPage();
await page.goto('https://httpbin.org/headers');
console.log(await page.textContent('body'));
await browser.close();
}
geotargetedSession().catch(console.error);Use Case: Testing localized e-commerce pricing, geo-restricted page content, and multi-step checkouts requiring static IPs.
4. Production Error Handling
Production-ready error handling pattern for JavaScript Playwright operations, featuring timeout settings, response checks, and guaranteed resource cleanup.
import { chromium } from 'playwright';
async function robustScraping(targetUrl) {
let browser = null;
try {
browser = await chromium.launch({
headless: true,
proxy: {
server: 'http://proxy.mrscraper.com:10000',
username: 'user-country-us-sessid-task1',
password: 'pass123'
}
});
const page = await browser.newPage();
page.setDefaultTimeout(30000); // 30s timeout
const response = await page.goto(targetUrl, { waitUntil: 'networkidle' });
if (!response || !response.ok()) {
throw new Error(`Failed to load page: ${response?.status()}`);
}
console.log('Page loaded successfully:', await page.title());
} catch (error) {
console.error(`Scraping failed for ${targetUrl}:`, error.message);
} finally {
if (browser) {
await browser.close();
}
}
}
robustScraping('https://example.com');Use Case: Deploying enterprise JavaScript Playwright scrapers that handle timeouts and failures gracefully.
Python
Prerequisites
pip install playwright
playwright install chromiumplaywright: Python package offering synchronous and asynchronous browser automation bindings.
Proxy Format
Playwright for Python passes proxy options via a dictionary to chromium.launch():
proxy = {
"server": "http://proxy.mrscraper.com:10000",
"username": "user-country-us-sessid-session1",
"password": "pass123"
}1. Basic Proxy Setup (Sync API)
Demonstrates the Python synchronous Playwright API for initializing an authenticated proxy connection and inspecting page output.
from playwright.sync_api import sync_playwright
def test_proxy():
with sync_playwright() as p:
# Launch browser with proxy server and credentials
browser = p.chromium.launch(
headless=True,
proxy={
"server": "http://proxy.mrscraper.com:10000",
"username": "user-country-us",
"password": "pass123"
}
)
page = browser.new_page()
page.goto("https://api.ipify.org?format=json")
print("Your Proxy IP:", page.text_content("body"))
browser.close()
if __name__ == "__main__":
test_proxy()Use Case: Quick proxy integration for Python automation scripts, test suites, and data collection utilities.
2. Sticky Session Management (Sync API)
Maintains a static Python Playwright browser session with a stable IP address across multi-step page navigations.
from playwright.sync_api import sync_playwright
def run_sticky_session():
with sync_playwright() as p:
# Static session IP in Canada
browser = p.chromium.launch(
headless=True,
proxy={
"server": "http://proxy.mrscraper.com:10000",
"username": "user-country-ca-sessid-auth1-sesstime-20",
"password": "pass123"
}
)
context = browser.new_context()
page = context.new_page()
for url in ["https://example.com/step1", "https://example.com/step2"]:
page.goto(url)
print(f"Visited {url} -> Title: {page.title()}")
browser.close()
if __name__ == "__main__":
run_sticky_session()Use Case: Simulating multi-step user interactions and login sequences in Python requiring a consistent residential IP.
3. Rotating Proxy Scraping (Async API)
Uses an asynchronous Python Playwright setup with rotating proxies to visit multiple target URLs, ensuring each request receives a fresh IP address.
import asyncio
from playwright.async_api import async_playwright
async def scrape_target(url, proxy_config):
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True, proxy=proxy_config)
page = await browser.new_page()
try:
await page.goto(url, wait_until="domcontentloaded", timeout=30000)
title = await page.title()
print(f"Scraped '{url}' -> Title: {title}")
except Exception as e:
print(f"Error scraping {url}: {e}")
finally:
await browser.close()
async def main():
# Rotating proxy (no sessid parameter)
proxy_config = {
"server": "http://proxy.mrscraper.com:10000",
"username": "user-country-uk",
"password": "pass123"
}
urls = [
"https://example.com/page1",
"https://example.com/page2",
"https://example.com/page3",
]
for url in urls:
await scrape_target(url, proxy_config)
if __name__ == "__main__":
asyncio.run(main())Use Case: High-volume automated Python browser scraping where rotating IPs prevent target site rate limits.
4. Production Error Handling & Async Teardown
Implements defensive Python async error trapping, default page timeout limits, and safe browser closing routines.
import asyncio
from playwright.async_api import async_playwright
async def safe_scrape(url):
proxy_config = {
"server": "http://proxy.mrscraper.com:10000",
"username": "user-country-us-sessid-prod1",
"password": "pass123"
}
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True, proxy=proxy_config)
try:
page = await browser.new_page()
page.set_default_timeout(30000)
response = await page.goto(url, wait_until="networkidle")
if not response or not response.ok:
raise RuntimeError(f"HTTP error {response.status if response else 'No response'}")
print(f"Successfully scraped {url} - Length: {len(await page.content())}")
except Exception as err:
print(f"Failed to scrape {url}: {err}")
finally:
await browser.close()
if __name__ == "__main__":
asyncio.run(safe_scrape("https://example.com"))Use Case: Enterprise Python async web scrapers operating in production environments with strict reliability requirements.
Notes
- Define proxy credentials when calling
chromium.launch()orbrowser.newContext()/browser.new_context(). - Use browser contexts to handle multiple proxy configurations within a single browser process.
- Keep sensitive proxy credentials in environment variables (
process.env.PROXY_PASSWORDoros.environ.get("PROXY_PASSWORD")).