Selenium启动Firefox报executable_path等弃用警告该如何解决?
问题原因
Selenium 4.0及以上版本不再支持直接向webdriver.Firefox()传入executable_path、firefox_binary参数,二者均已被废弃,需要通过Service对象和Options对象来分别配置驱动路径和浏览器路径。
修复步骤
- 新增导入Firefox对应的Service类和Options类
- 用Service类封装geckodriver驱动路径
- 用Options类封装Firefox浏览器二进制路径(如果是默认安装路径可以省略该配置)
- 初始化浏览器实例时传入构造好的service和options参数
完整修改后的代码示例
from bs4 import BeautifulSoup import requests from selenium import webdriver from selenium.webdriver.firefox.firefox_binary import FirefoxBinary # 新增导入Service、Options from selenium.webdriver.firefox.service import Service from selenium.webdriver.firefox.options import Options from selenium.webdriver.common.desired_capabilities import DesiredCapabilities import time from selenium.webdriver.common.keys import Keys import datetime as dt import urllib.parse # 配置geckodriver驱动服务 service = Service(executable_path='C:/Users/jinwoo/geckodriver.exe') # 配置Firefox浏览器选项 options = Options() # 替换原来的firefox_binary参数,不需要自定义路径可以删掉下行 options.binary = FirefoxBinary('你的Firefox浏览器实际安装路径') # 初始化浏览器实例 browser = webdriver.Firefox(service=service, options=options)
额外说明
如果你的Firefox是系统默认路径安装,不需要单独指定浏览器位置,可直接省略Options相关配置,简化写法如下:
service = Service(executable_path='C:/Users/jinwoo/geckodriver.exe') browser = webdriver.Firefox(service=service)
内容的提问来源于stack exchange,提问作者hey
相关产品推荐
相关产品推荐

