gpt4 book ai didi

Selenium + Google Colab 错误 : 'chromedriver' executable needs to be in PATH

转载 作者:行者123 更新时间:2023-12-04 00:02:48 25 4
gpt4 key购买 nike

我在这个线程 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 下查看,但找不到任何“chromium-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 失败。这可能是由于来自 Selenium 的更新。
您需要使用命令 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' executable needs to be in PATH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56829470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com