使用以下代码,在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类,但是webdriver没有任何服务对象,或者没有文档化。
任何帮助都是非常感谢的。
发布于 2022-03-12 21:03:03
这个错误信息..。
DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
browser = webdriver.Firefox(firefox_binary=binary,options=options)...implies表示,参数firefox_binary现在已被弃用,您需要传递一个Service对象。
详细信息
此错误与当前按照webdriver.py实现的per驱动程序是一致的
if firefox_binary:
warnings.warn('firefox_binary has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)解决方案
Options和Service参数外,不要使用所有参数。(#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")参考文献
您可以在以下网站找到几个相关的详细讨论:
https://stackoverflow.com/questions/71452131
复制相似问题