Undetected Chromedriver配置带认证代理失败问题求助
带认证代理在Undetected Chromedriver中配置失败的解决办法
问题重现
使用Undetected Chromedriver绕过Cloudflare检测时,配置带账号密码的代理始终失败:
- 直接在
--proxy-server参数中嵌入认证信息,访问目标网站后IP未变更 - 使用Selenium-Wire配置代理,浏览器仍弹出手动输入账号密码的认证窗口
尝试过的代码:
第一次尝试(直接配置ChromeOptions)
import undetected_chromedriver as uc from webdriver_manager.chrome import ChromeDriverManager options = uc.ChromeOptions() proxy = "xxx.xxx.xxx.xxx:xxxx:username:password" # 代理格式:ip:port:username:password ip, port, username, password = proxy.split(':') options.add_argument(f'--proxy-server=http://{username}:{password}@{ip}:{port}') driver = uc.Chrome(executable_path=ChromeDriverManager().install(), options=options) driver.get('https://2ip.ru/')
第二次尝试(使用Selenium-Wire)
from seleniumwire import undetected_chromedriver as uc from webdriver_manager.chrome import ChromeDriverManager options = uc.ChromeOptions() proxy = "xxx.xxx.xxx.xxx:xxxx:username:password" ip, port, username, password = proxy.split(':') wire_options = { 'proxy': { 'http': f'http://{username}:{password}@{ip}:{port}', 'https': f'http://{username}:{password}@{ip}:{port}', 'no_proxy': 'localhost,127.0.0.1' } } driver = uc.Chrome(executable_path=ChromeDriverManager().install(), options=options, seleniumwire_options=wire_options) driver.get('https://2ip.ru/')
问题原因
- Chrome安全限制:现代Chrome浏览器已禁止在
--proxy-server参数中直接明文传递用户名和密码,第一种方法因此完全失效。 - Selenium-Wire配置不规范:直接在代理URL中嵌入认证信息的方式,部分代理服务器不支持;且Selenium-Wire本身提供了单独配置认证字段的更可靠方式,之前的写法未利用该特性。
解决方案
方案1:使用Chrome扩展自动处理代理认证
创建简单Chrome扩展,自动拦截代理认证弹窗并填写账号密码:
新建扩展目录(如
proxy_auth_extension),包含两个文件:manifest.json:{ "manifest_version": 3, "name": "Proxy Auth AutoFill", "version": "1.0", "background": { "service_worker": "background.js" }, "permissions": ["webRequest", "webRequestAuthProvider", "<all_urls>"] }background.js:chrome.webRequest.onAuthRequired.addListener( function(details, callbackFn) { callbackFn({ authCredentials: { username: "你的代理用户名", password: "你的代理密码" } }); }, {urls: ["<all_urls>"]}, ["blocking"] );
在代码中加载扩展并配置代理:
import undetected_chromedriver as uc from webdriver_manager.chrome import ChromeDriverManager options = uc.ChromeOptions() proxy = "xxx.xxx.xxx.xxx:xxxx" # 仅需ip:port,无需带用户名密码 options.add_argument(f'--proxy-server=http://{proxy}') # 加载本地扩展 options.add_argument(r'--load-extension=./proxy_auth_extension') driver = uc.Chrome(executable_path=ChromeDriverManager().install(), options=options) driver.get('https://2ip.ru/')
方案2:正确配置Selenium-Wire的代理认证
使用Selenium-Wire提供的username和password字段单独配置认证,避免拼入URL:
from seleniumwire import undetected_chromedriver as uc from webdriver_manager.chrome import ChromeDriverManager options = uc.ChromeOptions() proxy = "xxx.xxx.xxx.xxx:xxxx:username:password" ip, port, username, password = proxy.split(':') wire_options = { 'proxy': { 'http': f'http://{ip}:{port}', 'https': f'http://{ip}:{port}', 'no_proxy': 'localhost,127.0.0.1', 'username': username, 'password': password } } driver = uc.Chrome(executable_path=ChromeDriverManager().install(), options=options, seleniumwire_options=wire_options) driver.get('https://2ip.ru/')
方案3:PAC文件配合扩展(可选)
若上述方法仍失效,可通过PAC文件指定代理,同时用方案1的扩展自动填充认证:
- 创建
proxy.pac文件:function FindProxyForURL(url, host) { return "PROXY xxx.xxx.xxx.xxx:xxxx; DIRECT"; } - 在代码中配置PAC文件:
import os from pathlib import Path pac_path = Path(os.getcwd()) / "proxy.pac" options.add_argument(f'--proxy-pac-url=file:///{pac_path.resolve()}')
内容的提问来源于stack exchange,提问作者qazya1
相关产品推荐
相关产品推荐

