python - DeprecationWarning : firefox_binary has been deprecated, 请使用 Selenium Python 中的参数 firefox_binary 传入 Service 对象

标签 python selenium firefox geckodriver tor

通过以下代码,我尝试在 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)

这实际上有效,但我收到以下警告:

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

我搜索了一种传递此 Service 对象的方法,但没有任何效果:基本上,我尝试像其他浏览器那样传递该对象。

事实上,虽然其他浏览器有记录 Service class ,即使我可以毫无错误地导入 from selenium.webdriver.firefox.service 一个 Service 类,webdriver 构造函数不具有任何服务对象,或者没有记录.

感谢任何帮助。

最佳答案

此错误消息...

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

...暗示参数 firefox_binary 现在已弃用,您需要传递 Service 改为对象。


详细信息

此错误与 webdriver 的当前实现一致,如 webdriver.py

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)

所以代替firefox_binary 你必须使用 binary_location 属性并将其传递给 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")

引用文献

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

关于python - DeprecationWarning : firefox_binary has been deprecated, 请使用 Selenium Python 中的参数 firefox_binary 传入 Service 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71452131/

相关文章:

python - 如何从 Ubuntu Dash 运行 Python 脚本?

python - 在 Linux 中 grep 列出文件中另一百万条记录中的 1000 条记录的最快方法

python - Pandas - 计算列值大于阈值限制的连续行

python - 如何使用 Python 和 Selenium 定位表格中的元素?

python - 如何将 browsermob 与 python-selenium 一起使用?

使用 firefox 浏览器调整 css

javascript - 从 firefox 扩展新加载的选项卡中列出并调用 js 函数

javascript - Firefox Javascript 调试器中的不良行为

python - 有条件地用不同 df 的值填充 pandas df 的列

selenium - NS_ERROR_SOCKET_ADDRESS_IN_USE