使用以下代碼,在 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 類,即使我可以沒有任何錯誤地匯入from selenium.webdriver.firefox.service一個Service類,webdriver 建構式沒有任何服務物件或者它沒有記錄。
任何幫助表示贊賞。
uj5u.com熱心網友回復:
此錯誤訊息...
DeprecationWarning: firefox_binary has been deprecated, please pass in a Service object
browser = webdriver.Firefox(firefox_binary=binary,options=options)
...意味著該引數現在已firefox_binary被棄用,您需要傳遞一個Service物件。
細節
根據webdriver.py,此錯誤與 webdriver 的當前實作行內
if firefox_binary:
warnings.warn('firefox_binary has been deprecated, please pass in a Service object',
DeprecationWarning, stacklevel=2)
解決方案
根據Selenium v??4.0 Beta 1:
- 棄用驅動程式實體化中除
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")
參考
您可以在以下位置找到一些相關的詳細討論:
- 將 Selenium 與用 Python 撰寫的 Brave Browser 傳遞服務物件一起使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/443348.html
上一篇:如何處理隱藏的選擇下拉選單
