FileNotFoundError:[Errno2]没有这样的文件或目录:'/content/drive/MyDrive/Santillana/geckodriver'使用Google Colab中的硒库

问题描述

我正尝试在Google Colab中使用Selenium,但在尝试运行Firefox实例时遇到一些错误。 我点击了以下链接:

  • Selenium documentation在这里,我尝试使用驱动程序管理软件,但收到错误消息,说找不到Firefox的二进制位置,所以我遵循了另一个link,但我无法使其运行

因此我尝试使用硬编码位置,但收到错误:

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

我实际上下载了Geckodrive,并遵循此link

我已通过google.colab库将grecodriver上传到我的驱动器和访问,但我总是收到错误:

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可执行文件。


分析

根据错误消息,您似乎在linux系统上。但是,您已下载windows版本的GeckoDriver:


解决方案

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

相关文章