Ещё можно сделать без selenium-wire. При открытии браузера загружается расширение, которое настраивает прокси с аутентификацией
import tempfile
import contextlib
from urllib.parse import urlparse
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class ChromeExtended(webdriver.Chrome):
def __init__(self, *args, options=None, proxy=None, **kwargs):
options = options or Options()
if proxy:
context = tempfile.TemporaryDirectory()
else:
context = contextlib.nullcontext()
with context as extensionDirpath:
self._setupProxy(extensionDirpath, proxy, options)
super().__init__(*args, options=options, **kwargs)
def _setupProxy(self, extensionDirpath, proxy, options):
if not proxy:
return
parsedProxy = urlparse(proxy)
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":"22.0.0"}'
background_js = 'var e={mode:"fixed_servers",rules:{singleProxy:{scheme:"%s",host:"%s",port:parseInt(%s)},bypassList:["localhost"]}};chrome.proxy.settings.set({value:e,scope:"regular"},(function(){})),chrome.webRequest.onAuthRequired.addListener((function(e){return{authCredentials:{username:"%s",password:"%s"}}}),{urls:["<all_urls>"]},["blocking"]);' \
% (parsedProxy.scheme, parsedProxy.hostname, parsedProxy.port, parsedProxy.username, parsedProxy.password)
with open(f"{extensionDirpath}/manifest.json", "w", encoding="utf8") as f:
f.write(manifest_json)
with open(f"{extensionDirpath}/background.js", "w", encoding="utf8") as f:
f.write(background_js)
options.add_argument(f"--load-extension={extensionDirpath}")
b = ChromeExtended(proxy="http://aaaaaaa:aaaaaa@100.100.100.100:8888")
b.get("https://www.whatismyip.com/")
input()