按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
相关产品推荐
相关产品推荐

