简体   繁体   English

DeprecationWarning:firefox_binary 已被弃用,请在 Selenium Python 中使用参数 firefox_binary 传入服务 object

[英]DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object using the argument firefox_binary in Selenium Python

With the following code, on a Mac I tried to start Tor browser with Python and Selenium:使用以下代码,在 Mac 上我尝试使用 Python 和 Selenium 启动 Tor 浏览器:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import os
import time

binary = '/Applications/Tor Browser.app/Contents/MacOS/firefox'

# binary location
if os.path.exists(binary) is False:
    raise ValueError("The binary path to Tor firefox does not exist.")
firefox_binary = FirefoxBinary(binary)

browser = None

def get_browser(binary=None, options=None):
    global browser
    # only one instance of a browser opens
    if not browser:
        browser = webdriver.Firefox(firefox_binary=binary, options=options)
    return browser

browser = get_browser(binary=firefox_binary)

time.sleep(20)
browser.get("http://stackoverflow.com")
time.sleep(10)
html = browser.page_source
print(html)

This actually WORKS, but I get the following warning:这实际上有效,但我收到以下警告:

DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
  browser = webdriver.Firefox(firefox_binary=binary,options=options)

I searched for a way to pass this Service object, but nothing worked: substantially, I tried to pass the object the way other browsers do.我搜索了一种方法来传递此Service object,但没有任何效果:实质上,我试图像其他浏览器一样传递 object。

In fact, while other browsers have a documented Service class , even if I can import without any error from selenium.webdriver.firefox.service a Service class, the webdriver constructor doesn't feature any service object or it is not documented.事实上,虽然其他浏览器有记录服务 class ,即使我可以from selenium.webdriver.firefox.service导入Service class 而没有任何错误,webdriver 构造函数不包含任何服务 object 或者它没有记录。

Any help is appreciated.任何帮助表示赞赏。

This error message...这个错误信息...

DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
  browser = webdriver.Firefox(firefox_binary=binary,options=options)

...implies that the argument firefox_binary is deprecated now and you need to pass a Service object instead. ...暗示现在不推荐使用firefox_binary 参数,您需要传递Service firefox_binary代替。


Details细节

This error is inline with the current implementation of webdriver as per webdriver.py根据webdriver.py ,此错误与 webdriver 的当前实现内联

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

Solution解决方案

As per Selenium v4.0 Beta 1 :根据Selenium v4.0 Beta 1

  • Deprecate all but Options and Service arguments in driver instantiation.在驱动程序实例化中弃用除OptionsService arguments 之外的所有内容。 (#9125,#9128) (#9125,#9128)

So instead of firefox_binary you have to use the binary_location property and pass it through an instance of FirefoxOptions() as follows:因此,您必须使用binary_location属性而不是 firefox_binary 并将其传递给firefox_binary FirefoxOptions()的实例,如下所示:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

option = webdriver.FirefoxOptions()
option.binary_location = r'/Applications/Tor Browser.app/Contents/MacOS/firefox'
driverService = Service('/path/to/geckodriver')
driver = webdriver.Firefox(service=driverService, options=option)
driver.get("https://www.google.com")

References参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DeprecationWarning: firefox_profile 已被弃用,请传入一个Service object - DeprecationWarning: firefox_profile has been deprecated, please pass in a Service object 如果 firefoxbinary 设置为 webdriver.Firefox(firefox_profile=profile, firefox_binary=binary),则 firefoxprofile 不起作用 - firefoxprofile not working if firefoxbinary is set as webdriver.Firefox(firefox_profile=profile, firefox_binary=binary) 弃用警告:firefox_profile 已被弃用,请传入选项 object - DeprecationWarning: firefox_profile has been deprecated, please pass in an Options object 弃用警告:desired_capabilities 已被弃用,请传入一个 Service 对象 - DeprecationWarning: desired_capabilities has been deprecated, please pass in a Service object DeprecationWarning:desired_capabilities 已被弃用,请使用 options kwarg super().__init__( - DeprecationWarning: desired_capabilities has been deprecated, please pass in an Options object with options kwarg super().__init__( 弃用警告:executable_path 已弃用 selenium python - DeprecationWarning: executable_path has been deprecated selenium python AttributeError: 'Options' object has no attribute 'binary' error invoking Headless Firefox using GeckoDriver through Selenium - AttributeError: 'Options' object has no attribute 'binary' error invoking Headless Firefox using GeckoDriver through Selenium InvalidArgumentException:消息:二进制不是使用 GeckoDriver Firefox Selenium 和 Python 的 Firefox 可执行文件错误 - InvalidArgumentException: Message: binary is not a Firefox executable error using GeckoDriver Firefox Selenium and Python 如何修复弃用警告:executable_path 已被弃用,请传入服务对象 - How to fix Deprecation Warning: executable_path has been deprecated, please pass in a Service object 两个问题,executable_path has been deprecated,请传入一个Service object problem and 'list' object has no attribute 'text' - Two questions, executable_path has been deprecated, please pass in a Service object problem and 'list' object has no attribute 'text'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM