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

Selenium搭配browsermobproxy报错FirefoxProfile无set_proxy属性如何解决

报错原因

这个错误是因为你使用的Selenium版本≥4.0,该版本已经完全移除了FirefoxProfile类的set_proxy方法,你查到的相关资料都是基于Selenium 3.x的旧版本写法,自然无法在新版本环境下运行。

解决方案

方案1:使用Selenium 4+ 原生支持的代理配置写法(推荐)

不需要调用旧的set_proxy方法,直接通过Options类配置代理即可,完整可运行代码如下:

from browsermobproxy import Server
from selenium import webdriver
from selenium.webdriver.firefox.options import Options

# 启动browsermobproxy
server = Server('path/to/your/browsermob-proxy') # 替换为你自己的browsermob-proxy路径
server.start()
proxy = server.create_proxy()

# Selenium4配置代理的正确写法
firefox_options = Options()
firefox_options.proxy = proxy.selenium_proxy()
driver = webdriver.Firefox(options=firefox_options)

# 原有业务逻辑不变
proxy.new_har("google")
driver.get("http://www.google.co.in")

# 资源释放
server.stop()
driver.quit()

如果需要兼容旧的FirefoxProfile写法,也可以把代理配置直接写入profile的偏好设置,代码如下:

profile = webdriver.FirefoxProfile()
selenium_proxy = proxy.selenium_proxy()
# 手动写入代理配置到profile
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", selenium_proxy.httpProxy.split(':')[0])
profile.set_preference("network.proxy.http_port", int(selenium_proxy.httpProxy.split(':')[1]))
profile.set_preference("network.proxy.ssl", selenium_proxy.sslProxy.split(':')[0])
profile.set_preference("network.proxy.ssl_port", int(selenium_proxy.sslProxy.split(':')[1]))
profile.set_preference("network.proxy.no_proxies_on", "")
driver = webdriver.Firefox(firefox_profile=profile)

方案2:降级Selenium到3.x版本

如果不想修改现有代码,可以直接降级Selenium到最后一个3.x稳定版,命令如下:
pip install selenium==3.141.0
降级后原有代码可以直接正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 12:36:05