Python Selenium 4 - Firefox FirefoxBinary() 已弃用

Python Selenium 4 - Firefox FirefoxBinary() Deprecated

我已经升级到 Selenium 4

new_binary_path = FirefoxBinary('path_to_binary')
selenium.webdriver.Firefox(executable_path=path, options=ops, firefox_binary=new_binary_path)

options.add_argument("--setBinary(path_to_binary)")
selenium.webdriver.Firefox(executable_path=path, options=ops)

Return这个错误信息

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

文档

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

Removed the firefox.Binary class. Custom binaries can still be selected using firefox.Options#setBinary(). Likewise, custom binary arguments can be specified with firefox.Options#addArguments()

有人知道如何实施这些更改吗?我不知道主题标签是什么意思。我试过 options.setBinary() 但无法识别 setBinary()

我已经解决了这个问题

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)

安装新版本后遇到了这个问题,解决方法如下。希望对其他朋友有所帮助

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options as options
from selenium.webdriver.firefox.options import Options as Firefox_Options

firefox_options = Firefox_Options()
firefox_options.binary = r'C:\Program Files\Mozilla Firefox\firefox.exe';

driver = webdriver.Firefox(executable_path=r'C:\xampp\htdocs\dev\geckodriver.exe',options=firefox_options)

driver.get('https://google.com')

这个错误信息...

DeprecationWarning: executable_path has been deprecated, please pass in a Service object

...暗示参数 现在您需要传递一个 Service 对象。


详情

此错误与 webdriver 的当前实现一致,并且根据 webdriver.py

if executable_path != DEFAULT_EXECUTABLE_PATH:
    warnings.warn('executable_path has been deprecated, please pass in a Service object',
                  DeprecationWarning, stacklevel=2)

另外:

if firefox_binary:
    warnings.warn('firefox_binary has been deprecated, please pass in a Service object',
                  DeprecationWarning, stacklevel=2)

解决方案

根据Selenium v4.0 Beta 1

  • Deprecate all but Options and Service arguments in driver instantiation. (#9125,#9128)

所以:

  • 而不是 executable_path 你必须使用 Service().

    的实例
  • 而不是 firefox_binary 你必须使用 binary_location 属性 并通过 FirefoxOptions().

    的实例传递它
  • 您可以使用以下代码块:

    from selenium import webdriver
    from selenium.webdriver.firefox.service import Service
    
    option = webdriver.FirefoxOptions()
    option.binary_location = '/path/to/firefox'
    driverService = Service('/path/to/geckodriver')
    driver = webdriver.Chrome(service=driverService, options=option)
    driver.get("https://www.google.com")
    

参考资料

您可以在以下位置找到一些相关的详细讨论:

  • Use Selenium with Brave Browser pass service object written in python