You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在undetected_chromedriver中启用带用户名密码认证的代理

问题根因

你当前的代理配置失效是三个明确原因导致的:

  • 你使用的undetected_chromedriver.v2是旧版兼容导入入口,在新版undetected-chromedriver(3.x版本)中,该入口会默认加载兼容层配置,启动时会重置网络相关参数,覆盖扩展写入的代理规则。
  • 你实现的代理插件基于Manifest V2规范,Chrome 110及以上版本已经默认拦截、禁用所有未上架的MV2扩展,插件不会实际加载运行,自然不会生效,也不会抛出报错。
  • 插件内使用的webRequest.onAuthRequired监听接口在Chrome新版本、undetected-chromedriver的默认隐私配置下,无法捕获代理认证请求,导致认证流程走不通。
修复方案

弃用Manifest V2插件的老旧实现方案,改用Chrome DevTools Protocol(CDP)原生能力处理代理配置和认证,不会被浏览器或undetected-chromedriver的配置覆盖,兼容性更强。
先升级依赖到最新版:
pip install -U undetected-chromedriver
然后替换为以下可直接运行的代码:

import undetected_chromedriver as uc
import time

# 代理参数
host = '91.201.40.130'
port = 12115
username = 'alos081100RnC5'
password = 'YRhSahmNYX'
proxy_server = f"http://{host}:{port}"

def get_chromedriver(use_proxy=False, user_agent=None):
    chrome_options = uc.ChromeOptions()
    if use_proxy:
        # 直接通过启动参数指定代理,优先级高于系统代理、扩展配置
        chrome_options.add_argument(f"--proxy-server={proxy_server}")
        # 关闭浏览器自动代理检测逻辑,避免覆盖手动配置
        chrome_options.add_argument("--disable-features=AutoDetectProxy")
    if user_agent:
        chrome_options.add_argument(f"--user-agent={user_agent}")

    # 启动时开启CDP事件支持
    driver = uc.Chrome(
        options=chrome_options,
        enable_cdp_events=True
    )

    if use_proxy:
        # 注册CDP代理认证回调,不需要扩展即可完成用户名密码校验
        def proxy_auth_handler(params):
            return {
                "authChallengeResponse": {
                    "response": "ProvideCredentials",
                    "username": username,
                    "password": password
                }
            }
        driver.add_cdp_listener("Fetch.authRequired", proxy_auth_handler)
        driver.execute_cdp_cmd("Fetch.enable", {"handleAuthRequests": True})

    return driver

def main():
    driver = get_chromedriver(use_proxy=True)
    driver.get("https://whatismyipaddress.com")
    # 停留10秒查看IP检测结果
    time.sleep(10)
    driver.quit()

if __name__ == "__main__":
    main()
注意事项
  • 运行前删除之前代码生成的proxy_auth_plugin.zip文件,避免残留扩展引发配置冲突。
  • 不要再使用import undetected_chromedriver.v2 as uc的导入方式,直接导入主模块即可,旧入口的兼容层会干扰网络配置。
  • 该方案不依赖任何浏览器扩展,不受Chrome Manifest V2弃用政策影响,在Chrome 100~最新版本均可正常工作。

内容的提问来源于stack exchange,提问作者Ahtesham Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 21:15:37