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

Selenium使用带认证轮换代理时出现net::ERR_TUNNEL_CONNECTION_FAILED错误求助

解决net::ERR_TUNNEL_CONNECTION_FAILED代理连接错误

这个错误本质是Chrome无法通过配置的代理建立隧道连接,核心问题集中在代理配置、认证或可用性上,按以下步骤排查修复:

  • 核对代理基础信息
    确认代理IP、端口完全正确,没有拼写错误。可以先手动在Chrome浏览器设置中添加代理(填入IP、端口、账号密码),尝试访问目标网站,排除代理本身的连通性问题。

  • 修正代理认证配置方式
    Selenium无法直接在Chrome选项中传入代理账号密码,必须通过扩展注入认证信息,新手常在这里出错。以下是可直接运行的代码示例:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import zipfile
    import os
    
    # 替换成你的代理信息
    proxy_host = "你的代理IP"
    proxy_port = "你的代理端口"
    proxy_username = "代理用户名"
    proxy_password = "代理密码"
    
    # 生成代理认证扩展的配置文件
    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Proxy Auth",
        "permissions": ["proxy", "tabs", "<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"}}, () => {{}});
    
    chrome.webRequest.onAuthRequired.addListener(
        details => {{return {{authCredentials: {{username: "{proxy_username}", password: "{proxy_password}"}}}};}},
        {{urls: ["<all_urls>"]}},
        ["blocking"]
    );
    """
    
    # 临时创建并打包扩展文件
    os.makedirs("tmp_proxy_ext", exist_ok=True)
    with open("tmp_proxy_ext/manifest.json", "w") as f:
        f.write(manifest_json)
    with open("tmp_proxy_ext/background.js", "w") as f:
        f.write(background_js)
    
    with zipfile.ZipFile("proxy_auth.zip", "w") as zp:
        zp.write("tmp_proxy_ext/manifest.json", "manifest.json")
        zp.write("tmp_proxy_ext/background.js", "background.js")
    
    # 加载扩展并启动浏览器
    chrome_options = Options()
    chrome_options.add_extension("proxy_auth.zip")
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://www.expressvpn.com/what-is-my-ip")
    
  • 验证代理可用性
    轮换代理可能存在IP失效情况,换一个代理IP测试,或用curl命令快速验证:

    curl -x http://用户名:密码@代理IP:端口 https://www.expressvpn.com/what-is-my-ip
    

    如果curl也报错,说明是代理服务商的问题,和代码无关。

  • 排除本地代理冲突
    关闭本地正在运行的VPN、代理软件,避免和代码中的代理配置冲突。

  • 匹配Chrome与ChromeDriver版本
    你的Chrome版本是110.0.5481.100,必须使用对应版本的ChromeDriver(110.x系列)。可以用webdriver-manager自动管理版本:

    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    # 替换原driver启动代码
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 19:20:53