Home > OS >  How to add proxy with auth to ubdetected chromdriver?
How to add proxy with auth to ubdetected chromdriver?

Time:01-21

I have to use undetected chromdriver for some websites, but I also need to add a proxy with authorization. The usual ways that work with regular chromdriver don't work with undetected. What I have tried:

1:

from seleniumwire import webdriver
import undetected_chromedriver as uc

options = webdriver.ChromeOptions()
seleniumwire_options = {'proxy': {'https': 'type://username:pass@host:port'}}
driver = uc.Chrome(use_subprocess=True, seleniumwire_options=seleniumwire_options)

2:

import undetected_chromedriver as uc

options = uc.ChromeOptions
options.add_argument("--proxy-server=https://username:pass@host:port")
driver = uc.Chrome(use_subprocess=True, options=options)

Are there other ways that work with undetected chromedriver? Thanks in advance

CodePudding user response:

SeleniumBase has a special undetected-chromedriver mode that lets you set the proxy settings via method arg (or on the command-line).

pip install seleniumbase, then run with python after you fill in the proxy settings on Line 3:

from seleniumbase import SB

with SB(uc=True, pls="none", proxy="USER:PASS@HOST:PORT") as sb:
    sb.open("https://nowsecure.nl/#relax")
    sb.assert_text("OH YEAH, you passed!", "h1")
    sb.post_message("Selenium wasn't detected!")

It sets uc to True (to enabled undetected-chromedriver mode), it sets pls to "none" (a different pageLoadStrategy might freeze up proxy tests in that mode), and it sets the proxy settings (after you fill in the one you want).

CodePudding user response:

This is the best method I have found.

import os
import shutil
import tempfile

import undetected_chromedriver as webdriver


class ProxyExtension:
    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome Proxy",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {"scripts": ["background.js"]},
        "minimum_chrome_version": "76.0.0"
    }
    """

    background_js = """
    var config = {
        mode: "fixed_servers",
        rules: {
            singleProxy: {
                scheme: "http",
                host: "%s",
                port: %d
            },
            bypassList: ["localhost"]
        }
    };

    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

    function callbackFn(details) {
        return {
            authCredentials: {
                username: "%s",
                password: "%s"
            }
        };
    }

    chrome.webRequest.onAuthRequired.addListener(
        callbackFn,
        { urls: ["<all_urls>"] },
        ['blocking']
    );
    """

    def __init__(self, host, port, user, password):
        self._dir = os.path.normpath(tempfile.mkdtemp())

        manifest_file = os.path.join(self._dir, "manifest.json")
        with open(manifest_file, mode="w") as f:
            f.write(self.manifest_json)

        background_js = self.background_js % (host, port, user, password)
        background_file = os.path.join(self._dir, "background.js")
        with open(background_file, mode="w") as f:
            f.write(background_js)

    @property
    def directory(self):
        return self._dir

    def __del__(self):
        shutil.rmtree(self._dir)


if __name__ == "__main__":
    proxy = ("64.32.16.8", 8080, "username", "password")  # your proxy with auth, this one is obviously fake
    proxy_extension = ProxyExtension(*proxy)

    options = webdriver.ChromeOptions()
    options.add_argument(f"--load-extension={proxy_extension.directory}")
    driver = webdriver.Chrome(options=options)

    driver.get("Just a moment...")
    driver.quit()
  • Related