【问题标题】:WebDriverException: Message: invalid argument: can't kill an exited process error with Selenium GeckoDriver and Firefox in Google Colab on UbuntuWebDriverException:消息:无效参数:无法在 Ubuntu 上的 Google Colab 中使用 Selenium GeckoDriver 和 Firefox 终止退出进程错误
【发布时间】:2019-12-26 01:58:03
【问题描述】:

在之前的帖子 (enter link description here) 中,以下代码(由 DebanjanB 提供)

# install firefox, geckodriver, and selenium
!apt-get update
!pip install selenium
!apt install firefox-geckodriver
!cp /usr/lib/geckodriver /usr/bin
!cp /usr/lib/firefox /usr/bin

from selenium import webdriver

binary = '/usr/bin/firefox'
options = webdriver.FirefoxOptions()
options.binary = binary
options.add_argument('start-maximized')
options.add_argument('--headless')
browser = webdriver.Firefox(firefox_options=options, executable_path='/usr/bin/geckodriver')
browser.get('http://google.com/')

这是正确的,导致版本问题。我不是 Ubuntu 方面的专家。

我使用 Google Colaboratory、Python3.6、Selenium3.14。

WebDriverException                        Traceback (most recent call last)
<ipython-input-2-f7828de5f77c> in <module>()
      4 options.add_argument('start-maximized')
      5 options.add_argument('--headless')
----> 6 browser = webdriver.Firefox(options=options, executable_path='/usr/bin/geckodriver')
      7 driver.get("https://www.google.com")
      8 print(driver.page_source)
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/firefox/webdriver.py in __init__(self, firefox_profile, firefox_binary, timeout, capabilities, proxy, executable_path, options, service_log_path, firefox_options, service_args, desired_capabilities, log_path, keep_alive)
    172                 command_executor=executor,
    173                 desired_capabilities=capabilities,
--> 174                 keep_alive=True)
    175 
    176         # Selenium remote

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
    155             warnings.warn("Please use FirefoxOptions to set browser profile",
    156                           DeprecationWarning, stacklevel=2)
--> 157         self.start_session(capabilities, browser_profile)
    158         self._switch_to = SwitchTo(self)
    159         self._mobile = Mobile(self)

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
    250         parameters = {"capabilities": w3c_caps,
    251                       "desiredCapabilities": capabilities}
--> 252         response = self.execute(Command.NEW_SESSION, parameters)
    253         if 'sessionId' not in response:
    254             response = response['value']

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

WebDriverException: Message: invalid argument: can't kill an exited process

我上网查了才发现这是Firefox和Geckodriver的版本冲突。但是没有人给出如何选择兼容版本的解决方案。这是我的问题。任何帮助表示赞赏。

【问题讨论】:

  • 你能把firefox_options=options改成options=options再重新测试一次吗?
  • 我之前做过,并且产生了相同的错误消息。参见第 6 行 ---->
  • 你能去掉--headless选项,让我知道观察结果吗?
  • 我掉了 --headless ,报错信息是一样的。
  • 我在 Google Colaboratory 同样的问题上试过了。谷歌浏览器工作正常。它与 firefox 而不是 geckodriver 相关,甚至 ..>firefox -v |更多的 。命令显示错误

标签: python-3.x ubuntu selenium-webdriver google-colaboratory geckodriver


【解决方案1】:

在 cmets 中进行了所有讨论之后,您似乎错误地下载了 GeckoDriver 的命令,并且可能在下载和/或安装 Firefox 时出现了一些问题。


GeckoDriver

在 中安装 的命令容易出错:

!apt install firefox-geckodriver

详细步骤如下:

  • 浏览到geckodriver releases page。为您的平台检查 GeckoDriver 的兼容版本并按如下方式下载:

    wget https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz
    
  • 解压文件:

    tar -xvzf geckodriver*
    
  • 使其可执行:

    chmod +x geckodriver
    
  • 将驱动程序添加到您的 PATH 以便其他工具可以找到它:

    export PATH=$PATH:/path-to-extracted-file/.
    

参考:How to install geckodriver in Ubuntu?


火狐

在所有受支持的 Ubuntu 版本中,最新版本的 Firefox 始终作为安全更新提供,正常更新将安装它。您需要启用安全性或者您需要更新存储库(例如 Software &amp; Updates -> Updates)。您需要默认启用Security,但最好检查包管理器是否默认不更新Firefox。

详细步骤见:

参考:How do I install the latest stable version of Firefox?


更新

Selenium Client Driver 页面提及:

所以可能 Python 3.7 仍然不受支持,您可能必须降级到相关版本。


结尾

SessionNotCreatedException: Message: Unable to create new service: ChromeDriverService with Selenium 3.14.0 and Python 3.7.3

【讨论】:

  • 在此处尝试您的步骤colab.research.google.com/notebooks/welcome.ipynb 问题与“Google Colaboratory”有关
  • @vesszabo 查看答案更新并告诉我状态。
  • 我安装了 Firefox 68.0.2 ,下载了 geckodriver 0.24.0,但我有 Selenium 3.141.0(Google Colab 拒绝安装 3.141.59)。我使用python3.7。错误信息是一样的。由于 Selenium 客户端和 WebDriver 语言绑定(seleniumhq.org/download),我也安装了 Selenium 3.14.0,但没有帮助。
  • @vesszabo 查看答案更新并告诉我状态。
  • 我检查了我在 Google Colaboratory 中使用的 Python 版本。 Python 3.6.8 64 位 [GCC 8.0.1 20180414(实验性)[主干修订版 259383]。对不起,不是我上面说的python3.7。
猜你喜欢
  • 2019-03-03
  • 1970-01-01
  • 2020-09-10
相关资源
最近更新 更多