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

求助:Selenium Python配置ChromeDriver代理失败,求有效方案

解决Selenium配置代理无效的问题

原脚本问题分析

原脚本依赖的DesiredCapabilities在Selenium 4及以上版本已被弃用,这是代理配置失效的核心原因之一。另外,免费代理稳定性极差,需先确认你从hidemy.name获取的代理IP:端口是否仍处于可用状态。

可用脚本实现

以下是适配Selenium 4+的代理配置脚本,支持HTTP/HTTPS代理:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

# 替换为你获取的有效代理地址
proxy_ip_port = '172.67.179.58:80'

# 配置Chrome启动选项
chrome_options = Options()
chrome_options.add_argument(f'--proxy-server=http://{proxy_ip_port}')

# 初始化浏览器(若ChromeDriver不在系统PATH中,需指定executable_path参数)
try:
    driver = webdriver.Chrome(options=chrome_options)
    driver.get('https://www.mio-ip.it/')
    time.sleep(5)  # 等待页面加载完成以查看IP
    # 可选:提取页面显示的IP并打印
    ip_display = driver.find_element("css selector", ".ip")
    print(f"当前代理IP: {ip_display.text}")
except Exception as e:
    print(f"执行出错: {str(e)}")
finally:
    driver.quit()

关键注意事项

  • 代理有效性验证:免费代理存活周期短,使用前可通过命令行测试,比如执行curl -x http://172.67.179.58:80 https://www.mio-ip.it/确认代理是否能正常转发请求。
  • 版本兼容性:确保ChromeDriver版本与本地Chrome浏览器版本完全匹配,避免启动失败或功能异常。
  • HTTPS代理适配:无需单独配置HTTPS代理,Chrome会自动通过指定的HTTP代理处理HTTPS请求,保持--proxy-server参数的http://前缀即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 03:22:47