Evet, Selenium ile kullanıcı adı/şifre isteyen (authenticated) proxy kullanmak gerçekten can sıkıcı olabiliyor çünkü browser bu bilgiyi otomatik almaz, popup çıkarır (HTTP Basic Auth). Normal webdriver.Proxy tanımıyla sadece IP:PORT girilebiliyor, auth kısmı girilemiyor.
Authenticated proxy için en sağlam yöntem, dinamik bir Chrome uzantısı (extension) oluşturmaktır. Bu uzantı, proxy'yi tanımlar ve Authorization header’ını ekler.
Aşağıdaki Python kodu, bu işi otomatik yapar, kullanabilirsiniz.
import zipfile
import os
def create_proxy_auth_extension(proxy_host, proxy_port, proxy_user, proxy_pass, plugin_path):
manifest_json = """
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy Auth Extension",
"permissions": [
"proxy", "tabs", "unlimitedStorage", "storage", "<all_urls>", "webRequest", "webRequestBlocking"
],
"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(plugin_path, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
# Kullanım
plugin_path = 'proxy_auth_plugin.zip'
create_proxy_auth_extension("proxy_ip", "proxy_port", "username", "password", plugin_path)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_extension(plugin_path)
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com")