Proxy Integration with Playwright

A playwright is the newest headless browser automation framework launched by Microsoft. It offers extensive API support for automation with significant programming language supports like Node JS, Python, Java, Typescript, and C Sharp. It allows us to use the high-level API to control Chromium, Firefox, or Webkit web drivers. You can use this to test your web applications, mobile, data scraping, etc. In this tutorial, We show playwright proxy integration with examples.

Install Playwright in Linux or Windows

You can download and install the Playwright in the Linux operating system using these commands:

For the Python version in Linux

pip install playwright

playwright install

For the NodeJS version in Linux

Use any of the package managers you use and install it

For Java version, follow this :

https://playwright.dev/java/docs/intro#installation

For the .Net version, follow:

https://playwright.dev/dotnet/docs/intro#installation

How to Configure proxy in Playwright

Playwright provide you with a network-level API to set the HTTP proxy setting. You can use the proxy server with the framework to make your requests.

Here is the Python code for the configuration example, and i use the dedicated private proxy from proxy-hub.

from playwright.async_api import async_playwright
import os
import asyncio


cwd = os.getcwd()


async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(
            proxy={
                "server": "http://144.168.138.109:7777",
                "username": "proxy-hub",
                "password": "proxies",
            }
        )
        page = await browser.new_page()
        await page.goto("https://www.proxy-hub.com/blog")
        print(await page.title())
        await page.screenshot(path=cwd + "/screenshot.png")
        await page.goto("https://httpbin.org/ip")
        print(await page.content())
        await browser.close()
       

asyncio.run(main())
Proxy with playwright
Result of the example code in the command line

I use python with the chromium browser and async function.

For the sync function, then import syncapi.

And remove the ‘await‘ keyword in the launch() method.

Two more package uses are os and asyncio.

launch() is the method to set the proxy in the browser context.

server = Your Host Proxy server with PORT

username = Username of the proxy server

password = Password of the proxy server

In this example, it visits Proxy-Hub blog print title and takes the screenshot. It goes to the httpnbin site to get the current ip used to ensure the private proxy is working.

Wrapping:

Browser automation becomes faster and more efficient with Playwright. However, Use this for web Scraping due to the lightweight and advanced settings option. Proxy solution for the Playwright is available and reach our team here.

Sharing Is Caring:

I'm an enthusiast about proxies, networking, linux .I've been working in the IT industry for over 10 years and have a lot of experience with different platforms and software. I've also been involved in many online projects, including developing SaaS sites. My goal is to help others learn about these tools and how to use them effectively.

Leave a Comment