Tuesday, April 29, 2025

How to Use a Proxy with Playwright

 You can assign a proxy server per browser instance in Playwright using JavaScript. This is useful for web scraping when you need to rotate IPs or access region-specific content:

How to Use a Proxy with Playwright


Example - Adding proxy to browser instance in playwright

import { test, expect } from '@playwright/test';
import { chromium } from 'playwright';

test('add proxy to browser instance', async () => {

const browser = await chromium.launch({
    headless: true, // Set to false if you want a visible browser
    proxy: { server: "11.11.11.1:9000" }, // Proxy server configuration
    // Optional: Add authentication
    // proxy: { server: '11.11.11.1:9000', username: 'user', password: 'pass' },
  });

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

  // Scrape or interact with the page here

  await browser.close();

});

You can assign a proxy for your browser instance when using Playwright. This is particularly useful for scraping region-specific content or avoiding IP bans. Proxies can be configured with or without authentication, depending on your needs.


No comments:

Post a Comment