Selenium WebDriver无法使用企业代理下载驱动问题求助
问题分析与解决方案
你的问题核心是:Selenium自动下载ChromeDriver时无法使用公司代理,但手动下载可以。之前配置的ChromeOptions代理参数仅作用于Chrome浏览器实例,而负责驱动下载的Selenium Manager组件并未读取这些设置,导致下载请求无法走代理。
错误点修正
原代码中--proxy-server参数存在格式错误:
- 多余的引号需要移除
- 协议格式应为
http://代理IP:端口,而非http=代理IP:端口
修正后的浏览器代理参数示例:
options.add_argument('--proxy-server=http://10.10.10.10:80')
解决方案
方案1:配置系统环境变量(推荐)
Selenium Manager会读取系统的HTTP_PROXY和HTTPS_PROXY环境变量,直接设置即可:
- Windows:
set HTTP_PROXY=http://10.10.10.10:80 set HTTPS_PROXY=http://10.10.10.10:80 - Linux/macOS:
export HTTP_PROXY=http://10.10.10.10:80 export HTTPS_PROXY=http://10.10.10.10:80
如果代理需要认证,格式为http://用户名:密码@10.10.10.10:80
方案2:在代码中为Selenium Manager配置代理
通过Service类为Selenium Manager单独设置环境变量,同时保留Chrome浏览器的代理配置:
from selenium import webdriver from selenium.webdriver.chrome.service import Service class seleniumOptions: def local_options(self): options = webdriver.ChromeOptions() options.add_argument('--no-sandbox') options.add_argument('--disable-web-security') options.add_argument('--disable-software-rasterizer') options.add_argument('--proxy-server=http://10.10.10.10:80') options.add_argument('--ignore-certificate-errors') options.add_experimental_option("detach", True) return options # 配置Selenium Manager的代理环境 service = Service() service.environment = { "HTTP_PROXY": "http://10.10.10.10:80", "HTTPS_PROXY": "http://10.10.10.10:80" } driver = webdriver.Chrome(options=seleniumOptions().local_options(), service=service)
方案3:手动指定ChromeDriver路径(绕过自动下载)
如果上述方法无效,可手动用公司代理下载对应版本的ChromeDriver,然后在代码中直接指定路径:
from selenium import webdriver from selenium.webdriver.chrome.service import Service class seleniumOptions: def local_options(self): options = webdriver.ChromeOptions() options.add_argument('--no-sandbox') options.add_argument('--disable-web-security') options.add_argument('--disable-software-rasterizer') options.add_argument('--proxy-server=http://10.10.10.10:80') options.add_argument('--ignore-certificate-errors') options.add_experimental_option("detach", True) return options # 手动指定已下载的ChromeDriver路径 service = Service(executable_path="/绝对路径/chromedriver") driver = webdriver.Chrome(options=seleniumOptions().local_options(), service=service)
内容的提问来源于stack exchange,提问作者Edwin
相关产品推荐
相关产品推荐

