Python 使用 Webdriver Manager 的 Selenium ChromeDriver 问题

Selenium ChromeDriver issue using Webdriver Manager for Python

当运行这段代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdrivermanager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().download_and_install())
driver.get("http://www.python.org")

这会导致安装 chromedriver 的行出现以下异常:

TypeError: expected str, bytes or os.PathLike object, not tuple

请注意,我知道已经有很多关于此主题的话题,但由于 webdrivermanager 似乎已经更新,以前的解决方案不起作用。

还有一个简短的旁注:我通过 conda 而不是 pip 安装了 webdrivermager。但这不必担心。

编辑:整个堆栈跟踪:

Traceback (most recent call last): File "C:\Users\stefa\OneDrive - Johannes Kepler Universität Linz\Dokumente\GitHub\briefly\src\crawler\crawler.py", line 19, in driver = webdriver.Chrome(ChromeDriverManager().download_and_install()) File "C:\Users\stefa\anaconda3\envs\briefly\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in init self.service.start() File "C:\Users\stefa\anaconda3\envs\briefly\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start self.process = subprocess.Popen(cmd, env=self.env, File "C:\Users\stefa\anaconda3\envs\briefly\lib\subprocess.py", line 951, in init self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\stefa\anaconda3\envs\briefly\lib\subprocess.py", line 1360, in _execute_child args = list2cmdline(args) File "C:\Users\stefa\anaconda3\envs\briefly\lib\subprocess.py", line 565, in list2cmdline for arg in map(os.fsdecode, seq): File "C:\Users\stefa\anaconda3\envs\briefly\lib\os.py", line 822, in fsdecode filename = fspath(filename) # Does type-checking of filename. TypeError: expected str, bytes or os.PathLike object, not tuple

您的代码块中存在以下两个问题:

  • 您需要从 webdriver_manager.chrome
  • 导入 ChromeDriverManager
  • 根据 Webdriver Manager for Python download_and_install() 不受支持,您必须使用 install()

因此您的有效代码块将是:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://www.python.org")

系统上,控制台输出将是:

C:\Users\Admin\Desktop\Python Programs>python webdriver-manager_ChromeDriverManager.py
[WDM] -

[WDM] - ====== WebDriver manager ======
[WDM] - Current google-chrome version is 95.0.4638
[WDM] - Get LATEST driver version for 95.0.4638
[WDM] - There is no [win32] chromedriver for browser 95.0.4638 in cache
[WDM] - Get LATEST driver version for 95.0.4638
[WDM] - Trying to download new driver from https://chromedriver.storage.googleapis.com/95.0.4638.54/chromedriver_win32.zip
[WDM] - Driver has been saved in cache [C:\Users\Admin\.wdm\drivers\chromedriver\win32.0.4638.54]

DevTools listening on ws://127.0.0.1:50921/devtools/browser/c26df2aa-67aa-4264-b1dc-34d6148b9174

You can find a relevant detailed discussion in

这是我的解决方案:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
s = Service('chromedriver/chromedriver96.exe')
driver = webdriver.Chrome(service=s, options=options)
  1. 为 Python 安装 Webdriver 管理器: 点安装 webdriver-manager
  2. 导入 ChromeDriverManager: 从 webdriver_manager.chrome 导入 ChromeDriverManager
  3. 使用网络driver: service = ChromeService(executable_path=ChromeDriverManager().install()) driver = webdriver.Chrome(服务=服务)