不推荐使用可执行路径的 Webdriver 调用

Webdriver call with executable path deprecated

当我在 Python 3.10 中 运行 我的 Selenium 4.1 脚本时,我收到警告 关键字参数 executable_path 已弃用的消息。 请参阅下面的脚本。

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

edge_path = 'edgedriver_win64/msedgedriver.exe'
driver = webdriver.Edge(executable_path=edge_path)
driver.get('https://bing.com')

element = driver.find_element(By.ID, 'sb_form_q')
element.send_keys('WebDriver')
element.submit()

time.sleep(10)
driver.close()
driver.quit()

警告信息:

script.py:13: DeprecationWarning: executable_path has been deprecated,
please pass in a Service object
  driver = webdriver.Edge(executable_path=edge_path)

我该如何解决这个问题?

要解决此问题,请为 Edge 网络驱动程序导入 Service class (下面的第 4 行),然后使用可执行文件创建一个服务对象 路径(下面的第 7 行)。 随后,将服务对象作为关键字参数传递 到 webdriver 创建调用(下面的第 8 行)。

示例脚本变为:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.edge.service import Service

edge_path = 'edgedriver_win64/msedgedriver.exe'
service = Service(executable_path=edge_path)
driver = webdriver.Edge(service = service)
driver.get('https://bing.com')

element = driver.find_element(By.ID, 'sb_form_q')
element.send_keys('WebDriver')
element.submit()

time.sleep(10)
driver.close()
driver.quit()