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

在Python的undetected-selenium中配置带认证代理报错求助

解决undetected-chromedriver带认证代理的配置错误

你的错误根源是undetected-chromedriver的Chrome类参数规则和原生selenium完全不同:原生selenium允许把浏览器驱动路径作为第一个位置参数,但undetected-chromedriver的构造函数第一个位置参数是options,驱动路径必须通过executable_path关键字参数传递,直接传字符串会被当成options处理,才会出现'str' object has no attribute '_session'的报错。另外你代码里导入了undetected_chromedriver as us但没实际使用,还是在用原生的webdriver.Chrome,这也是核心问题之一。

修正步骤:

  1. 替换原生selenium的Chrome类为undetected_chromedriver的Chrome类
  2. 用executable_path关键字参数指定驱动路径
  3. 补全PROXY_PORT的数值(你代码里这个变量是空的,必须填实际端口)

修正后的完整代码:

import undetected_chromedriver as us
from selenium.webdriver.common.by import By
from time import sleep
import random
import os
import zipfile

# 补全代理信息
PROXY_HOST = 'proxyip'  # 代理IP或主机
PROXY_PORT = 1234  # 替换为实际代理端口
PROXY_USER = 'user' # 代理用户名
PROXY_PASS = 'pass' # 代理密码


manifest_json = """{
    "version": "1.0.0",
    "manifest_version": 2,
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "unlimitedStorage",
        "storage",
        "<all_urls>",
        "webRequest",
        "webRequestBlocking"
    ],
    "background": {
        "scripts": ["background.js"]
    },
    "minimum_chrome_version":"22.0.0"
}"""

background_js = """var config = {
        mode: "fixed_servers",
        rules: {
        singleProxy: {
            scheme: "http",
            host: "%s",
            port: parseInt(%s)
        },
        bypassList: ["localhost"]
        }
    };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "%s",
            password: "%s"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
);""" % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)


def get_chromedriver(use_proxy=False, user_agent=None):
    path = os.path.dirname(os.path.abspath(__file__))
    # 使用undetected-chromedriver的ChromeOptions
    chrome_options = us.ChromeOptions()
    if use_proxy:
        pluginfile = 'proxy_auth_plugin.zip'

        with zipfile.ZipFile(pluginfile, 'w') as zp:
            zp.writestr("manifest.json", manifest_json)
            zp.writestr("background.js", background_js)
        chrome_options.add_extension(pluginfile)
    if user_agent:
        chrome_options.add_argument('--user-agent=%s' % user_agent)
    # 用executable_path指定驱动路径,调用undetected的Chrome类
    driver = us.Chrome(executable_path='my dir', options=chrome_options)
    return driver

def main():
    driver = get_chromedriver(use_proxy=True)
    driver.get('https://httpbin.org/ip')
    # 打印页面内容验证代理是否生效
    print(driver.page_source)
    driver.quit()

if __name__ == '__main__':
    main()

额外提示:

  • 如果你的undetected-chromedriver版本较新,它会自动查找系统中的Chrome驱动,此时可以直接省略executable_path参数
  • 代理扩展文件proxy_auth_plugin.zip运行后可以手动删除,避免残留
  • 确保代理的IP、端口、用户名、密码都是正确有效的

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 19:02:47