Python Selenium 4 - Firefox FirefoxBinary() 已弃用

Rhy*_*hys 10 selenium python-3.x selenium-firefoxdriver geckodriver

我已经升级到 Selenium 4

new_binary_path = FirefoxBinary('path_to_binary')
selenium.webdriver.Firefox(executable_path=path, options=ops, firefox_binary=new_binary_path)
Run Code Online (Sandbox Code Playgroud)

或者

options.add_argument("--setBinary(path_to_binary)")
selenium.webdriver.Firefox(executable_path=path, options=ops)
Run Code Online (Sandbox Code Playgroud)

返回此错误信息

DeprecationWarning: executable_path has been deprecated, please pass in a Service object
Run Code Online (Sandbox Code Playgroud)

文档

https://github.com/SeleniumHQ/selenium/blob/master/javascript/node/selenium-webdriver/CHANGES.md

删除了 firefox.Binary 类。仍然可以使用 firefox.Options#setBinary() 选择自定义二进制文件。同样,可以使用 firefox.Options#addArguments() 指定自定义二进制参数

有谁知道如何实施这些更改?我不知道标签是什么意思。我尝试过options.setBinary()setBinary()没有被识别。

Rhy*_*hys 14

我已经解决了问题

from selenium.webdriver.firefox.options import Options as options
from selenium.webdriver.firefox.service import Service

#///////////////// Init binary & driver
new_driver_path = 'path to driver'
new_binary_path = 'path to binary'

ops = options()
ops.binary_location = new_binary_path
serv = Service(new_driver_path)
browser1 = selenium.webdriver.Firefox(service=serv, options=ops)
Run Code Online (Sandbox Code Playgroud)