Skip to main content

Playwright Integration

Playwright is a powerful cross-browser automation library. It has built-in proxy support with authentication, making it ideal for use with WeProxies.

Installation

npm install playwright
npx playwright install

Or for Python:

pip install playwright
playwright install

JavaScript/TypeScript

Basic Usage

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

(async () => {
const browser = await chromium.launch({
proxy: {
server: 'http://proxy.weproxies.com:1080',
username: 'wp_user123',
password: 'your-password'
}
});

const page = await browser.newPage();
await page.goto('https://api.ipify.org?format=json');

const content = await page.textContent('body');
console.log(content);

await browser.close();
})();

Country Targeting

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

async function browseWithCountry(url, country) {
const browser = await chromium.launch({
proxy: {
server: 'http://proxy.weproxies.com:1080',
username: `wp_user123-country-${country}`,
password: 'password'
}
});

const page = await browser.newPage();
await page.goto(url);
const content = await page.textContent('body');

await browser.close();
return content;
}

// Usage
(async () => {
console.log('US:', await browseWithCountry('https://api.ipify.org?format=json', 'US'));
console.log('GB:', await browseWithCountry('https://api.ipify.org?format=json', 'GB'));
console.log('DE:', await browseWithCountry('https://api.ipify.org?format=json', 'DE'));
})();

Sticky Sessions

const { chromium } = require('playwright');
const { v4: uuidv4 } = require('uuid');

async function browseWithSession(country = null) {
const sessionId = uuidv4().replace(/-/g, '').slice(0, 12);

let username = 'wp_user123';
if (country) username += `-country-${country}`;
username += `-session-${sessionId}`;

const browser = await chromium.launch({
proxy: {
server: 'http://proxy.weproxies.com:1080',
username,
password: 'password'
}
});

const page = await browser.newPage();

// All requests use the same IP
for (let i = 0; i < 3; i++) {
await page.goto('https://api.ipify.org?format=json');
const ip = await page.textContent('body');
console.log(`Request ${i + 1}: ${ip}`);
}

await browser.close();
}

browseWithSession('US');

Cross-Browser Testing

const { chromium, firefox, webkit } = require('playwright');

async function testWithBrowser(browserType, name) {
const browser = await browserType.launch({
proxy: {
server: 'http://proxy.weproxies.com:1080',
username: 'wp_user123-country-US',
password: 'password'
}
});

const page = await browser.newPage();
await page.goto('https://api.ipify.org?format=json');
const content = await page.textContent('body');

console.log(`${name}: ${content}`);
await browser.close();
}

(async () => {
await testWithBrowser(chromium, 'Chrome');
await testWithBrowser(firefox, 'Firefox');
await testWithBrowser(webkit, 'Safari');
})();

Python

Basic Usage

from playwright.sync_api import sync_playwright

def main():
with sync_playwright() as p:
browser = p.chromium.launch(
proxy={
"server": "http://proxy.weproxies.com:1080",
"username": "wp_user123",
"password": "password"
}
)

page = browser.new_page()
page.goto("https://api.ipify.org?format=json")

content = page.text_content("body")
print(content)

browser.close()

main()

Async Usage

import asyncio
from playwright.async_api import async_playwright

async def main():
async with async_playwright() as p:
browser = await p.chromium.launch(
proxy={
"server": "http://proxy.weproxies.com:1080",
"username": "wp_user123-country-US",
"password": "password"
}
)

page = await browser.new_page()
await page.goto("https://api.ipify.org?format=json")

content = await page.text_content("body")
print(content)

await browser.close()

asyncio.run(main())

Multi-Country Scraping

import asyncio
from playwright.async_api import async_playwright

async def scrape_country(playwright, url, country):
browser = await playwright.chromium.launch(
proxy={
"server": "http://proxy.weproxies.com:1080",
"username": f"wp_user123-country-{country}",
"password": "password"
}
)

try:
page = await browser.new_page()
await page.goto(url, timeout=30000)
content = await page.text_content("body")
return {"country": country, "content": content}
except Exception as e:
return {"country": country, "error": str(e)}
finally:
await browser.close()

async def main():
countries = ["US", "GB", "DE", "FR", "JP"]
url = "https://api.ipify.org?format=json"

async with async_playwright() as p:
tasks = [scrape_country(p, url, country) for country in countries]
results = await asyncio.gather(*tasks)

for result in results:
print(f"{result['country']}: {result.get('content') or result.get('error')}")

asyncio.run(main())

Advanced Features

Screenshot and PDF

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

(async () => {
const browser = await chromium.launch({
proxy: {
server: 'http://proxy.weproxies.com:1080',
username: 'wp_user123-country-US',
password: 'password'
}
});

const page = await browser.newPage();
await page.goto('https://example.com');

// Screenshot
await page.screenshot({ path: 'screenshot.png', fullPage: true });

// PDF
await page.pdf({ path: 'page.pdf', format: 'A4' });

await browser.close();
})();

Request Interception

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

(async () => {
const browser = await chromium.launch({
proxy: {
server: 'http://proxy.weproxies.com:1080',
username: 'wp_user123',
password: 'password'
}
});

const page = await browser.newPage();

// Block images and fonts for faster loading
await page.route('**/*', route => {
const resourceType = route.request().resourceType();
if (['image', 'font', 'stylesheet'].includes(resourceType)) {
route.abort();
} else {
route.continue();
}
});

await page.goto('https://example.com');
await browser.close();
})();

Context Isolation

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

(async () => {
const browser = await chromium.launch({
proxy: {
server: 'http://proxy.weproxies.com:1080',
username: 'wp_user123',
password: 'password'
}
});

// Create isolated contexts for different sessions
const context1 = await browser.newContext({
userAgent: 'User Agent 1'
});

const context2 = await browser.newContext({
userAgent: 'User Agent 2'
});

const page1 = await context1.newPage();
const page2 = await context2.newPage();

// Cookies and storage are isolated
await page1.goto('https://example.com');
await page2.goto('https://example.com');

await browser.close();
})();

Complete Scraper Example

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

class PlaywrightScraper {
constructor(username, password) {
this.username = username;
this.password = password;
this.browser = null;
}

async init(country = null, session = null) {
let user = this.username;
if (country) user += `-country-${country}`;
if (session) user += `-session-${session}`;

this.browser = await chromium.launch({
headless: true,
proxy: {
server: 'http://proxy.weproxies.com:1080',
username: user,
password: this.password
}
});
}

async scrape(url, options = {}) {
const context = await this.browser.newContext({
viewport: { width: 1920, height: 1080 },
userAgent: options.userAgent || 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
});

const page = await context.newPage();

if (options.blockResources) {
await page.route('**/*', route => {
if (['image', 'font', 'stylesheet'].includes(route.request().resourceType())) {
route.abort();
} else {
route.continue();
}
});
}

try {
await page.goto(url, {
waitUntil: 'networkidle',
timeout: options.timeout || 30000
});

if (options.waitForSelector) {
await page.waitForSelector(options.waitForSelector);
}

const content = options.getText
? await page.textContent('body')
: await page.content();

if (options.screenshot) {
await page.screenshot({ path: options.screenshot });
}

return { success: true, content };
} catch (error) {
return { success: false, error: error.message };
} finally {
await context.close();
}
}

async close() {
if (this.browser) {
await this.browser.close();
}
}
}

// Usage
(async () => {
const scraper = new PlaywrightScraper('wp_user123', 'password');
await scraper.init('US', 'my-session');

const result = await scraper.scrape('https://api.ipify.org?format=json', {
getText: true,
blockResources: true
});

console.log(result);
await scraper.close();
})();

Best Practices

1. Use Headless Mode in Production

const browser = await chromium.launch({
headless: true,
proxy: { ... }
});

2. Set Reasonable Timeouts

await page.goto(url, { timeout: 30000 });
await page.waitForSelector('.element', { timeout: 10000 });

3. Handle Errors Gracefully

try {
await page.goto(url);
} catch (error) {
if (error.name === 'TimeoutError') {
console.log('Navigation timed out');
}
}

4. Clean Up Resources

try {
// Scraping code
} finally {
await context.close();
await browser.close();
}