You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何运行Python Selenium脚本时出现NoSuchDriverException错误?

Selenium ChromeDriver 初始化失败问题排查

我已在笔记本电脑上学习Python编程数周,现希望在台式机上运行一个基于Selenium的程序。我下载了ChromeDriver并尝试执行,但始终出现错误。

运行的代码:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('google.com')

报错日志:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\selenium_manager.py:137, in SeleniumManager.run(args)
    136 stderr = completed_proc.stderr.decode("utf-8").rstrip("\n")
---> 137 output = json.loads(stdout)
    138 result = output["result"]

File ~\AppData\Local\anaconda3\Lib\json\__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
---> 346     return _default_decoder.decode(s)
    347 if cls is None:

File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\selenium_manager.py:103, in SeleniumManager.driver_location(self, options)
    101     args.append(value)
---> 103 output = self.run(args)
    105 browser_path = output["browser_path"]

File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\selenium_manager.py:140, in SeleniumManager.run(args)
    139 except Exception as err:
---> 140     raise WebDriverException(f"Unsuccessful command executed: {command}") from err
    142 for item in output["logs"]:

WebDriverException: Message: Unsuccessful command executed: C:\Users\Ben\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser chrome --output json


The above exception was the direct cause of the following exception:

NoSuchDriverException                     Traceback (most recent call last)
Cell In[13], line 2
      1 from selenium import webdriver
----> 2 driver = webdriver.Chrome()
      3 driver.get('google.com')

File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\chrome\webdriver.py:45, in WebDriver.__init__(self, options, service, keep_alive)
     42 service = service if service else Service()
     43 options = options if options else Options()
---> 45 super().__init__(
     46     browser_name=DesiredCapabilities.CHROME["browserName"],
     47     vendor_prefix="goog",
     48     options=options,
     49     service=service,
     50     keep_alive=keep_alive,
     51 )

File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\chromium\webdriver.py:49, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, options, service, keep_alive)
     37 """Creates a new WebDriver instance of the ChromiumDriver. Starts the
     38 service and then creates new WebDriver instance of ChromiumDriver.
     39 
   (...)
     45  - keep_alive - Whether to configure ChromiumRemoteConnection to use HTTP keep-alive.
     46 """
     47 self.service = service
---> 49 self.service.path = DriverFinder.get_path(self.service, options)
     50 self.service.start()
     52 executor = ChromiumRemoteConnection(
     53     remote_server_addr=self.service.service_url,
     54     browser_name=browser_name,
   (...)
     57     ignore_proxy=options._ignore_local_proxy,
     58 )

File ~\AppData\Local\anaconda3\Lib\site-packages\selenium\webdriver\common\driver_finder.py:41, in DriverFinder.get_path(service, options)
     39 except Exception as err:
     40     msg = f"Unable to obtain driver for {options.capabilities['browserName']} using Selenium Manager."
---> 41     raise NoSuchDriverException(msg) from err
     43 if path is None or not Path(path).is_file():
     44     raise NoSuchDriverException(f"Unable to locate or obtain driver for {options.capabilities['browserName']}")

NoSuchDriverException: Message: Unable to obtain driver for chrome using Selenium Manager.; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location

我尝试过使用不同版本的ChromeDriver,但都没有解决问题。而在我的笔记本电脑上运行相同的脚本却完全正常。


解决方法

  • 手动指定ChromeDriver路径:
    跳过Selenium Manager自动检测,直接指定下载好的ChromeDriver路径。确保ChromeDriver版本和Chrome浏览器大版本一致(比如Chrome是118.x,Driver也用118.x)。代码示例:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    
    # 替换为你下载的ChromeDriver的实际路径
    driver_path = r"C:\path\to\chromedriver.exe"
    service = Service(driver_path)
    driver = webdriver.Chrome(service=service)
    driver.get("https://google.com")
    
  • 检查Chrome浏览器安装路径:
    Selenium Manager可能找不到非默认路径安装的Chrome,手动指定Chrome二进制文件路径:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    # 替换为你的Chrome.exe实际路径
    options.binary_location = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
    driver = webdriver.Chrome(options=options)
    driver.get("https://google.com")
    
  • 更新Selenium版本:
    旧版本Selenium Manager可能存在兼容性问题,执行命令更新:

    pip install --upgrade selenium
    
  • 检查权限问题:
    尝试以管理员身份运行Python脚本或命令提示符,避免权限限制导致Driver无法下载或执行。

  • 验证ChromeDriver可执行性:
    直接双击下载的chromedriver.exe,若弹出缺失DLL的错误,安装对应的Visual C++ Redistributable运行库。

内容的提问来源于stack exchange,提问作者Ben Troutman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 10:57:23