Python中Selenium Firefox WebDriver代理配置无效问题求助
解决Firefox WebDriver代理IP不生效的问题
核心问题:参数不兼容
你用的--proxy-server是Chrome浏览器的启动参数,Firefox WebDriver不识别这个参数,所以代理根本没生效,自然返回真实IP。
正确的设置方法
Firefox需要通过修改浏览器偏好配置来设置代理,以下是两种可行方案:
方案1:用FirefoxOptions配置偏好
直接在FirefoxOptions中添加代理相关的配置项:
from selenium import webdriver options = webdriver.FirefoxOptions() proxy_host = "137.74.65.101" proxy_port = "80" # 开启手动代理模式 options.set_preference("network.proxy.type", 1) # 设置HTTP代理 options.set_preference("network.proxy.http", proxy_host) options.set_preference("network.proxy.http_port", int(proxy_port)) # 设置HTTPS代理(如果需要) options.set_preference("network.proxy.https", proxy_host) options.set_preference("network.proxy.https_port", int(proxy_port)) # 禁止本地地址绕过代理(可选,确保所有请求走代理) options.set_preference("network.proxy.no_proxies_on", "") driver = webdriver.Firefox(options=options) driver.get('https://httpbin.org/ip')
方案2:用FirefoxProfile配置(兼容旧版本)
如果你的Selenium版本偏旧,可以用FirefoxProfile来配置:
from selenium import webdriver from selenium.webdriver.firefox.options import Options profile = webdriver.FirefoxProfile() proxy_host = "137.74.65.101" proxy_port = 80 profile.set_preference("network.proxy.type", 1) profile.set_preference("network.proxy.http", proxy_host) profile.set_preference("network.proxy.http_port", proxy_port) profile.set_preference("network.proxy.https", proxy_host) profile.set_preference("network.proxy.https_port", proxy_port) profile.set_preference("network.proxy.no_proxies_on", "") options = Options() options.profile = profile driver = webdriver.Firefox(options=options) driver.get('https://httpbin.org/ip')
额外排查要点
- 先验证代理IP有效性:用curl或手动在浏览器配置该代理,访问httpbin.org/ip确认是否能切换IP,排除代理本身失效的问题。
- 检查系统/浏览器代理干扰:关闭Firefox的自动代理设置,避免系统代理覆盖程序配置。
- 确认版本匹配:确保Selenium、geckodriver和Firefox浏览器版本相互兼容,版本不匹配可能导致配置不生效。
内容的提问来源于stack exchange,提问作者esteban garnil
相关产品推荐
相关产品推荐

