以前我写过一个使用Selenium操作Chromium的教程,今晚想写个自动抢票小程序来着,发现有报错:

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\driver_finder.py:38, in DriverFinder.get_path(service, options)
     37 try:
---> 38     path = SeleniumManager().driver_location(options) if path is None else path
     39 except Exception as err:

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\selenium_manager.py:95, in SeleniumManager.driver_location(self, options)
     93     args.append(value)
---> 95 output = self.run(args)
     97 browser_path = output["browser_path"]

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\selenium_manager.py:141, in SeleniumManager.run(args)
    140 if completed_proc.returncode:
--> 141     raise WebDriverException(f"Unsuccessful command executed: {command}.\n{result}{stderr}")
    142 return result

WebDriverException: Message: Unsuccessful command executed: C:\Users\xxx\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\common\windows\selenium-manager.exe --browser chrome --browser-path C:\Users\xxx\jupyterbooks\chrome-win\chrome.exe --output json.
{'code': 65, 'message': 'error sending request for url (https://storage.googleapis.com/chrome-for-testing-public/126.0.6437.0/win64/chromedriver-win64.zip): error trying to connect: 远程主机强迫关闭了一个现有的连接。 (os error 10054)', 'driver_path': '', 'browser_path': ''}


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

NoSuchDriverException                     Traceback (most recent call last)
Cell In[8], line 4
      2 opts = Options()
      3 opts.binary_location = chromium_path
----> 4 driver = webdriver.Chrome(options=opts)

File ~\AppData\Local\Programs\Python\Python310\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     DesiredCapabilities.CHROME["browserName"],
     47     "goog",
     48     options,
     49     service,
     50     keep_alive,
     51 )

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\chromium\webdriver.py:51, in ChromiumDriver.__init__(self, browser_name, vendor_prefix, options, service, keep_alive)
     47 self.vendor_prefix = vendor_prefix
     49 self.service = service
---> 51 self.service.path = DriverFinder.get_path(self.service, options)
     53 self.service.start()
     55 try:

File ~\AppData\Local\Programs\Python\Python310\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与chromium版本不匹配)。

chromedriver官网:https://chromedriver.chromium.org/

最新版下载地址:https://googlechromelabs.github.io/chrome-for-testing/

chromium官网:https://www.chromium.org/Home/

最新版下载地址:https://download-chromium.appspot.com/

把下载好的chromedriver放在Python安装目录里,和python.exe平级即可。