【问题标题】:WebDriverException: Message: Service /content/chromedriver unexpectedly exited. Status code was: -6 with ChromeDriver Google Colab and SeleniumWebDriverException:消息:服务 /content/chromedriver 意外退出。状态码是:-6 使用 ChromeDriver Google Colab 和 Selenium
【发布时间】:2019-05-01 02:31:54
【问题描述】:

我试图使用 Selenium 运行无头 Chrome 浏览器以从网络上抓取内容。我使用 wget 安装了无头 Chrome,然后在我当前的文件夹中解压缩。

!wget "http://chromedriver.storage.googleapis.com/2.25/chromedriver_linux64.zip"
!unzip chromedriver_linux64.zip

现在我正在加载驱动程序

from selenium.webdriver.chrome.options import Options
import os
# instantiate a chrome options object so you can set the size and headless preference
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")

chrome_driver = os.getcwd() +"/chromedriver"
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chrome_driver)

我遇到了一个错误

WebDriverException                        Traceback (most recent call last)
<ipython-input-67-0aeae0cfd891> in <module>()
----> 1 driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
  2 driver.get("https://www.google.com")
  3 lucky_button = driver.find_element_by_css_selector("[name=btnI]")
  4 lucky_button.click()
  5 /usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, chrome_options, service_args, desired_capabilities, service_log_path)
 60             service_args=service_args,
 61             log_path=service_log_path)
---> 62         self.service.start()
 63 
 64         try:

 /usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in start(self)
 84         count = 0
 85         while True:
 ---> 86             self.assert_process_still_running()
 87             if self.is_connectable():
 88                 break

 /usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in assert_process_still_running(self)
 97             raise WebDriverException(
 98                 'Service %s unexpectedly exited. Status code was: %s'
 ---> 99                 % (self.path, return_code)
100             )
101 

WebDriverException: Message: Service /content/chromedriver unexpectedly exited. Status code was: -6

更新

所以经过一番研究,我尝试了另一种方式

!apt install chromium-chromedriver
import selenium as se

options = se.webdriver.ChromeOptions()
options.add_argument('headless')

driver = se.webdriver.Chrome(chrome_options=options)

在 Google Colab 上再次给我同样的错误

WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -6

【问题讨论】:

  • @DebanjanB 这三个问题没有与此问题相关的答案。操作系统不同(Win 与 Linux),没有公认的答案。
  • 没有一个答案与错误代码-6有关。我尝试了所有方法。在阅读整个问题之前,请不要标记重复。
  • @KorakotChaovavanich 主要错误是 chromedriver 意外退出。不同的OS 和不同的Selenium Language Binding Arts 将针对相同的错误显示不同的Status code。我已将 OP 指向最相关的讨论。如果您还有其他疑虑,请告诉我。
  • 美汤不能刮掉javascript生成的内容
  • 请分享一个能重现您观察到的问题的笔记本。

标签: python selenium google-chrome selenium-webdriver google-colaboratory


【解决方案1】:

我已经找到了关于我为什么会出错的问题的答案。请安装 chromium-chromedriver 并将其添加到您的路径变量以及 bin 目录中。

这是如何在 Colab 上使用 Selenium 抓取数据的问题的完整解决方案。还有另一种使用 PhantomJS 的方法,但是 Selenium 已弃用此 API,希望他们会在下一次 Selenium 更新中将其删除。

# 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

这适用于任何想要在 Google Colab 上而不是在本地计算机上抓取数据的人。请按相同顺序依次执行所示步骤。

你可以在这里找到笔记本https://colab.research.google.com/drive/1GFJKhpOju_WLAgiVPCzCGTBVGMkyAjtk

【讨论】:

  • 来自警告“弃用警告:使用选项而不是 chrome_options”。是否有理由使用 chrome_options 而不是选项?
  • 另外,我不认为 sys.path.insert() 是必要的。 sys.path 用于搜索 python 模块,而不是可执行文件。
  • No sys.path.insert() 不是必需的,您可以将其删除。我已经编辑了我的答案。而对于选项,我找不到它新发布的类似功能。
  • @KorakotChaovavanich 我认为此语句 webdriver.ChromeOptions() 存在问题,但真正的原因在其他地方。顺便谢谢。
【解决方案2】:

此错误消息...

WebDriverException: Message: Service /content/chromedriver unexpectedly exited. Status code was: -6

...暗示 ChromeDriver 意外退出。

您的主要问题是您使用的二进制文件版本之间的不兼容性,如下所示:

  • 按照代码行:

    !wget "http://chromedriver.storage.googleapis.com/2.25/chromedriver_linux64.zip"
    
  • 您正在使用 chromedriver=2.25

  • chromedriver=2.25 的发行说明明确提及以下内容:

支持 Chrome v53-55

  • 虽然您没有提到 Chrome 浏览器 的版本,但您应该使用的是最新的 Chrome 浏览器 版本之一。

因此,ChromeDriver v2.33 与最近发布的 Chrome 浏览器 版本之间存在明显的不匹配。

解决方案


更新

我不确定google-colaboratory。底线是您必须使用与当前安装的 Google Chrome 版本相匹配的 ChromeDriver 版本。

但是,您需要先找到一种在 Colab 上安装 Chrome 或 Chromium 的方法。然后,您可以使用!wget!unzip下载解压并开始使用匹配的ChromeDriver 版本。

您可以在discussion

中找到关于 ChromeDriverChrome 浏览器 之间兼容性的讨论

【讨论】:

  • 我在 colab 上做这个。为什么我需要在云端安装谷歌浏览器。
  • @DebanjanB 请为“Google Colaboratory”提供相关答案。这是一个免费的 Jupyter Notebook 服务,运行 ubuntu 17,colab.research.google.com 没有“项目工作区”,没有 IDE,没有重建,没有 Revo。你不能像笔记本电脑一样重启它。
  • @HimanshuPoddar 我不确定google-colaboratory。底线是您必须根据现行 Google Chrome 版本的版本使用相关的 ChromeDriver
  • 在 Colab 中,您只能通过!apt!apt-get!wget!pip install 安装东西。不能与命令行交互,所以需要使用 -y 选项。
  • @DebanjanB 您在此处所说的“只需使用您的测试环境中安装的 Google Chrome 的现有安装即可。”完全是错误的。请改正。剩下的有用,谢谢!
【解决方案3】:

这可能不会直接帮助您。但是如果最终无法安装 Chrome + selenium,你仍然可以使用 phantomjs + selenium。喜欢这个笔记本:

https://colab.research.google.com/drive/1V62zhjw2V5buxdN1s9mqkLzh3FWqSq8S

但如果可能的话,我更喜欢 Chrome。

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 2018-08-25
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 2017-11-30
    相关资源
    最近更新 更多