首页 > 解决方案 > Python Selenium Chrome Webdriver 未安装

问题描述

我正在尝试自动安装最新版本的 Chrome 驱动程序,然后将其用于我的脚本,但遇到了错误。关于这里有什么问题的任何想法?我的缓存有什么东西吗?

driver2 = webdriver.Chrome(ChromeDriverManager().install())
options = selenium.webdriver.ChromeOptions()
#options.add_argument('headless')
options.add_argument('window-size=1920x1080')
driver = webdriver.Chrome(driver2, options=options)

错误:

[WDM] - Looking for [chromedriver 89.0.4389.23 win32] driver in cache 
[WDM] - File found in cache by path [C:\Users\xxx\.wdm\drivers\chromedriver\89.0.4389.23\win32\chromedriver.exe]
Traceback (most recent call last):
  File "C:\Users\xxx\Python\Price Tracking\Real Estate\RealEstate-Scraping.py", line 60, in <module>
    driver = webdriver.Chrome(driver2, options=options)
  File "C:\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
    self.process = subprocess.Popen(cmd, env=self.env,
  File "C:\Python38\lib\subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Python38\lib\subprocess.py", line 1247, in _execute_child
    args = list2cmdline(args)
  File "C:\Python38\lib\subprocess.py", line 549, in list2cmdline
    for arg in map(os.fsdecode, seq):
  File "C:\Python38\lib\os.py", line 818, in fsdecode
    filename = fspath(filename)  # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not WebDriver

标签: pythonseleniumselenium-webdriverselenium-chromedriver

解决方案


你需要告诉webdriver的路径:

webdriver.chrome(executable_path=*path*,options=options)

driver2 = webdriver.Chrome(ChromeDriverManager().install()) 创建一个新的硒实例。

driver = webdriver.Chrome(ChromeDriverManager().install(),options=options)

应该适用于您的用例 - 您的代码的第一行不是必需的。

请注意,“无头”也需要在它前面加上“--”。

完整代码:

options = selenium.webdriver.ChromeOptions()
#options.add_argument('--headless')
#could also do options.headless = True
options.add_argument('--window-size=1920x1080')
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.get('enterwebsite.ext')
#do other stuff

推荐阅读