SeleniumBase+无痕Chrome代理认证弹窗问题求助
SeleniumBase undetected Chrome模式下带认证代理的弹窗问题
使用SeleniumBase开启uc=True(undetected Chrome)模式时,配置带用户名密码认证的代理后,代理认证弹窗反复弹出,无法正常使用代理访问目标网站。
当前使用的代码配置如下:
chrome_options = { "headless": False, "uc": True, "incognito": True } chrome_options["proxy"] = f"{username}:{password}@{ip}:{port}" # Initialize the driver with authenticated proxy driver = Driver(**chrome_options) try: # Open the target website driver.get("https://ipinfo.io/json") print("Current IP info:", driver.page_source) sleep(10) finally: # Quit the driver driver.quit()
已尝试但无效的方案:
- 直接在
proxy参数中传入带凭证的代理地址,未被正确识别 - 使用
selenium-authenticated-proxy包,无效 - 创建后台运行的Chrome扩展实现代理认证,无效
- 搜索GitHub上相关方案,均无法稳定运行
有效解决方案:适配undetected Chrome的代理认证扩展
由于uc=True模式下,undetected-chromedriver会对Chrome启动参数做特殊处理,普通扩展加载方式可能被拦截,需调整扩展创建和加载逻辑,适配SeleniumBase的启动流程:
import os import zipfile from seleniumbase import Driver # 替换为你的代理信息 PROXY_HOST = "你的代理IP" PROXY_PORT = "你的代理端口" PROXY_USER = "代理用户名" PROXY_PASS = "代理密码" # 生成代理认证扩展(采用Manifest V2,适配undetected Chrome) manifest_json = """ { "version": "1.0.0", "manifest_version": 2, "name": "Proxy Auth", "permissions": [ "proxy", "tabs", "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() {{}}); function handleAuthRequired(details) {{ return {{ authCredentials: {{ username: "{PROXY_USER}", password: "{PROXY_PASS}" }} }}; }} chrome.webRequest.onAuthRequired.addListener( handleAuthRequired, {{urls: ["<all_urls>"]}}, ["blocking"] ); """ # 创建扩展压缩包 def create_proxy_extension(): plugin_path = "proxy_auth.zip" with zipfile.ZipFile(plugin_path, "w") as zp: zp.writestr("manifest.json", manifest_json) zp.writestr("background.js", background_js) return plugin_path # 初始化Driver并加载扩展 proxy_ext = create_proxy_extension() chrome_options = { "headless": False, "uc": True, "incognito": True, "extension": proxy_ext # 使用SeleniumBase原生参数加载扩展 } driver = Driver(**chrome_options) try: driver.get("https://ipinfo.io/json") print("当前IP信息:", driver.page_source) input("按回车退出...") finally: driver.quit() # 清理临时文件 if os.path.exists(proxy_ext): os.remove(proxy_ext)
关键注意点:
- 用SeleniumBase自带的
extension参数加载扩展,适配uc=True的启动逻辑 - 扩展采用Manifest V2,避免V3在undetected Chrome中的兼容性问题
- 若代理为HTTPS协议,需将
background.js中的scheme改为"https" - 扩展会自动拦截代理认证请求并填充凭证,无需手动操作弹窗
内容的提问来源于stack exchange,提问作者john Lennon
相关产品推荐
相关产品推荐

