FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Santillana/geckodriver' using Selenium libary in Google Colab

FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Santillana/geckodriver' using Selenium libary in Google Colab

我正在尝试在 Google Colab 中使用 Selenium,但是当我尝试 运行 Firefox 实例时出现一些错误。 我点击了这个链接:

所以我尝试使用硬编码位置,但出现错误:

Message: 'geckodriver' executable needs to be in PATH.

我实际上下载了 geckodrive 并按照这个 link

我已经将 grecodriver 上传到我的驱动器并使用 google.colab 库访问,但我总是遇到错误:

No such file or directory: '/content/drive/MyDrive/Santillana/geckodriver'

这是我的代码:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from webdriver_manager.firefox import GeckoDriverManager
from google.colab import drive

drive.mount('/content/drive', force_remount=True)

service = FirefoxService(executable_path= '/content/drive/MyDrive/Santillana/geckodriver')
driver = webdriver.Firefox(service=service)

整个错误:

---------------------------------------------------------------------------

FileNotFoundError                         Traceback (most recent call last)

/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/service.py in start(self)
     75                                             stdin=PIPE,
---> 76                                             creationflags=self.creationflags)
     77         except TypeError:

4 frames

FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Santillana/geckodriver': '/content/drive/MyDrive/Santillana/geckodriver'


During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)

/usr/local/lib/python3.7/dist-packages/selenium/webdriver/common/service.py in start(self)
     81                 raise WebDriverException(
     82                     "'%s' executable needs to be in PATH. %s" % (
---> 83                         os.path.basename(self.path), self.start_error_message)
     84                 )
     85             elif err.errno == errno.EACCES:

WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 

这个错误信息...

FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Santillana/geckodriver': '/content/drive/MyDrive/Santillana/geckodriver'

...表示您的程序无法找到 GeckoDriver 可执行文件。


分析

根据错误消息,您似乎在 system. However you have downloaded the version of GeckoDriver:


解决方案

而不是 Windows 版本的 GeckoDriver 可能您需要下载 Linux 版本的 GeckoDriver 可执行文件。