1

I am currently trying to get a cross browser testing solution from BrowserStack to work with all our company frameworks.

For Selenium this works fine by adding os.environ['HTTPS_PROXY'] = HTTPS_PROXY_ADDRESS

But for Playwright this does not work and it tries to bypass our company proxy and connect directly, which fails because our firewall does not allow this.

BrowserStack has an example in JS that does work so this is not impossible.

But I cannot get this to work in Python. I have tried setting HTTP_PROXY, HTTPS_PROXY, WS_PROXY and WSS_PROXY without success.

Does anyone know how I can force Playwright to use our proxy?

3 Answers 3

0

I'm currently using proxy in playwright and this is how i use it

from playwright.sync_api import sync_playwright


def initiate_browser():
    p = sync_playwright().start()
    browser = p.firefox.launch(headless=True)
    page = browser.new_page(proxy={"server": "ip_of_your_server"})
    return page
Sign up to request clarification or add additional context in comments.

2 Comments

This is how to make the browser started by Playwright use a proxy. But it does not make the initial call out to start the browser use a proxy.
Are you using firefox?
0

You can try passing the below Python equivalent of the sample JS that you shared from the BrowserStack link which you mentioned is working:

os.environ['GLOBAL_AGENT_HTTP_PROXY'] = 'http://someuser:[email protected]:3128'

1 Comment

Adding this setting does not solve the problem.
0

I use yarl for url parsing, but you can use classic urllib.

import os
from yarl import URL  # or urllib.parse
from playwright.async_api import async_playwright  # or sync_playwright



def proxy_config(self) -> dict:
        proxy_url = os.environ.get('HTTP_PROXY') or os.environ.get('HTTPS_PROXY')

        config = {}

        if proxy_url:
            constructed_proxy_url = URL(proxy_url)
            config = {
                'server': str(constructed_proxy_url),
                'username': constructed_proxy_url.user,
                'password': constructed_proxy_url.password,
            }

        return config


async with async_playwright() as p:
    browser = await p.chromium.launch(headless=True)
    if proxy_config():
        page = await browser.new_page(proxy=settings.proxy_config)
    else:
        page = await browser.new_page()

    await page.goto('https://www.python.org/', wait_until='networkidle')
    await asyncio.sleep(10)

    content = await page.content()

    await browser.close()
    print(content)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.