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

Python Selenium配置Proxy后无法更换IP的问题求助

Selenium代理未生效的问题修复

问题核心原因

  1. 配置顺序错误:你在初始化uc.Chrome实例之后才向option添加代理及其他参数,此时配置已经无法作用于已创建的浏览器实例。
  2. 代理格式不完整:提供的代理仅包含IP,缺少必要的端口号,浏览器无法建立代理连接。

修正后的代码

import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options

# 代理必须是「IP:端口」格式,替换为你实际可用的代理端口
PROXY = "139.162.214.146:8080"

option = Options()
# 所有配置参数必须在创建浏览器实例前添加
option.add_argument(f'--proxy-server={PROXY}')
option.add_argument('--disable-extensions')
option.add_argument('--disable-notifications')
option.add_argument('--no-sandbox')
option.add_argument('--disable-dev-shm-usage')
option.add_argument('--disable-gpu')
option.add_argument('--mute-audio')
option.add_argument('--ignore-certificate-errors')
option.add_argument('--disable-logging')

# 最后初始化浏览器实例
driver = uc.Chrome(options=option)
driver.get("https://www.whatismyip.com/")

input()

额外注意事项

  • 确保代理服务器处于可用状态,能正常访问目标网站;部分代理需要账号密码验证,需额外添加对应配置(如通过Chrome扩展或手动设置代理认证)。
  • 若使用SOCKS5代理,需明确指定协议类型:--proxy-server=socks5://IP:端口。
  • 可添加--incognito参数启用无痕模式,避免浏览器缓存或本地配置干扰代理生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 00:05:28