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

按Selenium文档配置的Firefox代理代码失效,真实IP仍暴露

Firefox代理配置不生效的解决方法

直接修改webdriver.DesiredCapabilities.FIREFOX的方式在新版Selenium(4.x及以上)中已不再适用,这是代理设置未生效的核心原因。以下是两种可靠的配置方案:

方案一:使用Proxy类绑定到FirefoxOptions

from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType

PROXY = "<HOST:PORT>"

# 初始化代理对象并配置参数
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = PROXY
proxy.ssl_proxy = PROXY
proxy.ftp_proxy = PROXY

# 将代理配置注入FirefoxOptions
options = webdriver.FirefoxOptions()
options.proxy = proxy

with webdriver.Firefox(options=options) as driver:
    driver.get("https://whatismyipaddress.com/")

方案二:直接设置Firefox偏好参数

这种方式绕过Selenium封装,直接配置Firefox原生代理设置,兼容性更强:

from selenium import webdriver

PROXY = "<HOST:PORT>"
proxy_host, proxy_port = PROXY.split(":")

options = webdriver.FirefoxOptions()
# 配置HTTP和HTTPS代理地址与端口
options.set_preference("network.proxy.http", proxy_host)
options.set_preference("network.proxy.http_port", int(proxy_port))
options.set_preference("network.proxy.ssl", proxy_host)
options.set_preference("network.proxy.ssl_port", int(proxy_port))
# 设置代理模式为手动
options.set_preference("network.proxy.type", 1)
# 禁用本地地址跳过代理(确保所有请求走代理)
options.set_preference("network.proxy.no_proxies_on", "")

with webdriver.Firefox(options=options) as driver:
    driver.get("https://whatismyipaddress.com/")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 06:01:03