Selenium+Google Colab错误:chromedriver'可执行文件需要在PATH中 [英] Selenium + Google Colab error: 'chromedriver' executable needs to be in PATH

查看:16
本文介绍了Selenium+Google Colab错误:chromedriver'可执行文件需要在PATH中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个线程How to use Selenium on Colaboratory Google?中使用了@Korakot-chaovavanich提供的代码。我真的需要一个解决方案来让它在Google Colab上工作。

# install chromium, its driver, and selenium
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
wd = webdriver.Chrome('chromedriver',options=options)
wd.get("https://www.google.com")
print(wd.page_source)  # results

我在Google Colab中测试过,但我不知道为什么它不能工作。我试图在/usr/lib下查找,但找不到任何"Chrome-Browser"文件夹。我找不到ChromeDriver在Google Colab中的安装位置。

在Google Colab中执行此代码的错误消息:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  libnvidia-common-410
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
  chromium-browser chromium-browser-l10n chromium-codecs-ffmpeg-extra
Suggested packages:
  webaccounts-chromium-extension unity-chromium-extension adobe-flashplugin
The following NEW packages will be installed:
  chromium-browser chromium-browser-l10n chromium-chromedriver
  chromium-codecs-ffmpeg-extra
0 upgraded, 4 newly installed, 0 to remove and 16 not upgraded.
Need to get 68.5 MB of archives.
After this operation, 251 MB of additional disk space will be used.
Ign:1 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 chromium-codecs-ffmpeg-extra amd64 74.0.3729.169-0ubuntu0.18.04.1
Ign:2 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 chromium-browser amd64 74.0.3729.169-0ubuntu0.18.04.1
Ign:3 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 chromium-browser-l10n all 74.0.3729.169-0ubuntu0.18.04.1
Ign:4 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 chromium-chromedriver amd64 74.0.3729.169-0ubuntu0.18.04.1
Err:1 http://security.ubuntu.com/ubuntu bionic-updates/universe amd64 chromium-codecs-ffmpeg-extra amd64 74.0.3729.169-0ubuntu0.18.04.1
  404  Not Found [IP: 91.189.88.31 80]
Err:2 http://security.ubuntu.com/ubuntu bionic-updates/universe amd64 chromium-browser amd64 74.0.3729.169-0ubuntu0.18.04.1
  404  Not Found [IP: 91.189.88.31 80]
Err:3 http://security.ubuntu.com/ubuntu bionic-updates/universe amd64 chromium-browser-l10n all 74.0.3729.169-0ubuntu0.18.04.1
  404  Not Found [IP: 91.189.88.31 80]
Err:4 http://security.ubuntu.com/ubuntu bionic-updates/universe amd64 chromium-chromedriver amd64 74.0.3729.169-0ubuntu0.18.04.1
  404  Not Found [IP: 91.189.88.31 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/chromium-codecs-ffmpeg-extra_74.0.3729.169-0ubuntu0.18.04.1_amd64.deb  404  Not Found [IP: 91.189.88.31 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/chromium-browser_74.0.3729.169-0ubuntu0.18.04.1_amd64.deb  404  Not Found [IP: 91.189.88.31 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/chromium-browser-l10n_74.0.3729.169-0ubuntu0.18.04.1_all.deb  404  Not Found [IP: 91.189.88.31 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/chromium-chromedriver_74.0.3729.169-0ubuntu0.18.04.1_amd64.deb  404  Not Found [IP: 91.189.88.31 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
cp: cannot stat '/usr/lib/chromium-browser/chromedriver': No such file or directory
Requirement already satisfied: selenium in /usr/local/lib/python3.6/dist-packages (3.141.0)
Requirement already satisfied: urllib3 in /usr/local/lib/python3.6/dist-packages (from selenium) (1.24.3)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in start(self)
     75                                             stderr=self.log_file,
---> 76                                             stdin=PIPE)
     77         except TypeError:

4 frames
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver': 'chromedriver'

During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)
/usr/local/lib/python3.6/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: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

推荐答案

问题源于Google Chrome和Chromium浏览器版本不匹配,导致命令apt install chromium-chromedriver失败。这可能是由于硒的更新。 您需要使用apt-get update命令更新每个包,以避免您的工具版本不匹配。 在CoLab上试用:

# install chromium, its driver, and selenium
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
wd = webdriver.Chrome('chromedriver',options=options)
wd.get("https://www.website.com")
print(wd.page_source)  # results

这篇关于Selenium+Google Colab错误:chromedriver'可执行文件需要在PATH中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆