Ошибка Selenium + Google Colab: исполняемый файл chromedriver должен находиться в PATH

Я использую код, предложенный @ korakot-chaovavanich в этом потоке Как использовать Selenium в совместной лаборатории Google?. Мне действительно нужно решение, чтобы оно работало в 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". Не могу понять, куда в Google Colab устанавливается chromedriver.

Сообщение об ошибке при выполнении этого кода в 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

person Christopher Chua    schedule 01.07.2019    source источник
comment
похоже, что драйвер Chrome не установлен должным образом. попробуйте установить его вручную и соответственно укажите путь.   -  person Murthi    schedule 01.07.2019
comment
Извините, как мне установить его вручную? Это на Google Colab.   -  person Christopher Chua    schedule 02.07.2019


Ответы (1)


Проблема возникает из-за несоответствия версий между Google Chrome и Chromium Browser, что приводит к сбою команды 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
person SyllAI    schedule 02.07.2019
comment
Решение работает отлично. Я поддержал ваш ответ, но в настоящее время у меня менее 15 репутации, поэтому он не отображается. Большое тебе спасибо! - person Christopher Chua; 05.07.2019