pythonselenium-webdriver

Proxy python selenium


how can i configurate proxy in selenium python, I am using the last version of python and selenium I am tryng that

proxy = Proxy({"proxyType": ProxyType.AUTODETECT})
client_config = ClientConfig(remote_server_addr=grid_server,
                             proxy=proxy,
                             timeout=3600,
                             ignore_certificates=True,
                             username="admin", password="myStrongPassword")
options = get_default_chrome_options()
driver = webdriver.Remote(command_executor=grid_server, options=options, client_config=client_config)
driver.get("https://www.selenium.dev")
driver.quit()

but i dont know how can i put the data of proxy i don't know that put in remote_server_addr=grid_server ,proxy=proxy,username="admin", password="myStrongPassword"


Solution

  • You’re trying to configure a proxy with authentication in Selenium using a Remote WebDriver (Selenium Grid), but the challenge is that Selenium’s --proxy-server argument does not support username and password authentication by default.

    Option 1

    You need to bypass this by using methods like Selenium Wire
    https://www.zenrows.com/blog/selenium-proxy#proxy-authentication-selenium
    PS: Its no longer maintained but i'm not sure about an alternative

    Option 2

    Create a Chrome Extension to inject proxy authentication.
    Step 1 :create an script to create the extension

    import zipfile
    
    PROXY_HOST = "proxyserver.com"
    PROXY_PORT = "8080"
    PROXY_USER = "admin"
    PROXY_PASS = "myStrongPassword"
    
    def create_proxy_extension():
        manifest_json = """{
            "version": "1.0.0",
            "manifest_version": 2,
            "name": "Chrome Proxy",
            "permissions": ["proxy", "webRequest", "webRequestBlocking", "<all_urls>"],
            "background": {"scripts": ["background.js"]}
        }"""
    
        background_js = f"""
        var config = {{
            mode: "fixed_servers",
            rules: {{
                singleProxy: {{
                    scheme: "http",
                    host: "{PROXY_HOST}",
                    port: parseInt({PROXY_PORT})
                }},
                bypassList: ["localhost"]
            }}
        }};
        chrome.proxy.settings.set({{value: config, scope: "regular"}}, function() {{}});
        chrome.webRequest.onAuthRequired.addListener(
            function(details) {{
                return {{
                    authCredentials: {{
                        username: "{PROXY_USER}",
                        password: "{PROXY_PASS}"
                    }}
                }};
            }},
            {{urls: ["<all_urls>"]}},
            ["blocking"]
        );
        """
    
        with zipfile.ZipFile("proxy_auth_plugin.zip", "w") as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
    
    create_proxy_extension()
    

    Step 2

    load the extension into Selenium Chrome WebDriver:

    
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_extension("proxy_auth_plugin.zip")
    driver = webdriver.Chrome(service=Service("/path/to/chromedriver"), options=chrome_options)
    driver.get("https://www.selenium.dev")
    driver.quit()