Integrate Playwright with ProxyPanel

2023-09-11 14:26

CTRL+K
Thumbnail

Introduction

Playwright — An Overview

Playwright is a versatile open-source automation library developed by Microsoft, offering a robust framework for end-to-end testing and web scraping. It allows developers to interact with web pages through a high-level API, supporting multiple browsers, including Chromium, Firefox, and WebKit. Playwright excels in automating browser tasks with high reliability and efficiency, making it an ideal choice for web scraping, testing, and automation. For more detailed information and documentation, you can visit the official Playwright documentation.

Prerequisites

Install Python from the official website.

Check if Python is installed correctly, you can use the following command in your terminal or command prompt

python --version

The output should be in the following format:

Python 3.11.2

If you receive an error or see a version number starting with 2.x, you need to download Python 3.x and follow the installation instructions to set it up

To get started with this tutorial, you'll need to install the Playwright Python library. You can install it using the following pip command:

pip install playwright

After installing the Playwright library, install the necessary browsers using Playwright. Run the following command to download and install the required browser binaries:

playwright install

Now that we're ready to use Playwright, let's grab our IP address using the following address:

											from playwright.async_api import async_playwright
import asyncio

async def main():
    async with async_playwright() as playwright:
        browser = await playwright.chromium.launch()
        context = await browser.new_context()
        page = await context.new_page()

        # Navigate to the URL that returns your IP address
        await page.goto("https://httpbin.org/ip")

        # Extract the IP address from the page content
        req = await page.inner_text('body')  # Adjust the selector as needed
        print(req)

        # Close the context and browser
        await context.close()
        await browser.close()

# Run the asynchronous function
asyncio.run(main())

										
  • URL: We navigate to https://httpbin.org/ip, which returns the IP address in JSON format.

  • Extract Content: We use inner_text('body') to get the text content of the page. Adjust the selector if necessary, based on the structure of the returned content.

After running the script, it sends a GET request to http://httpbin.io/ip and retrieves your public IP address from the response. The IP address is then printed out in JSON format.

Now, we've used Playwright without integrating ProxyPanel proxies. Follow the next steps to configure your proxy with Playwright.

Configuring the Playwright Package with ProxyPanel

Go to your Dashboard panel and navigate to the "My Proxy" section to view your IP information.

Click on the "Show Password" button and enter your account password to display your proxy password.

Dashboard-Credentials

To integrate ProxyPanel proxy details into your Playwright configuration, you need to pass these details directly when launching the browser, as shown in the following code:

from playwright.async_api import async_playwright
import asyncio

async def main():
    async with async_playwright() as playwright:
        # Launch the browser with proxy settings
        browser = await playwright.chromium.launch(
            proxy={
                'server': '154.198.34.247:8083',  # Replace with your proxy server and port
                'username': 'john.MyProxy-1',                # Replace with your proxy username
                'password': 'Proxypanel!123',                # Replace with your proxy password
            }
        )
        context = await browser.new_context()
        page = await context.new_page()

        # Navigate to the URL that returns your IP address
        await page.goto("https://httpbin.org/ip")

        # Get and print the page content
        req = await page.inner_text('body')  # Adjust the selector as needed
        print(req)

        # Close the context and browser
        await context.close()
        await browser.close()

# Run the asynchronous function
asyncio.run(main())

Replace username, password, proxy_address, and port with your ProxyPanel credentials and proxy details.

You will notice that the IP address with proxy configuration differs from the one without a proxy. Now you have successfully integrated the Playwright package with ProxyPanel.