DeprecationWarning: desired_capabilities 已被弃用,请传入一个Service对象

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

也许有人遇到了这个问题... 我为 PyTest 使用自定义 'chrome driver' 和 'performance' log:

cap = webdriver.DesiredCapabilities.CHROME.copy()
cap['goog:loggingPrefs'] = {'performance': 'ALL'}
services = Service(executable_path='/usr/local/bin/chromedriver')
chrome_driver = webdriver.Chrome(desired_capabilities=cap, options=options, service=services)

我的环境:

现在我收到警告:

=============================== warnings summary ===============================
tests/test_name.py::test_main[/]
  /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py:69: DeprecationWarning: desired_capabilities has been deprecated, please pass in a Service object
    super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",

-- Docs: https://docs.pytest.org/en/stable/warnings.html
======================== 1 passed, 1 warning in 10.49s =========================

可能有人知道如何处理这个?

见下文

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService

options = webdriver.ChromeOptions()
options.set_capability("loggingPrefs", {'performance': 'ALL'})
service = ChromeService(executable_path'/usr/local/bin/chromedriver')
driver = webdriver.Chrome(service=service, options=options)

如果上述方法不起作用,请尝试:

options.set_capability("goog:loggingPrefs", {'performance': 'ALL'})