为什么ChromeDriver 2.31在Docker中运行时无法启动?

时间:2017-08-21 09:13:51

标签: selenium docker

我有一个基于ubuntu:xenial的Docker容器。当我尝试使用selenium webdriver无限加载地运行Chrome

>>> from selenium import webdriver
>>> from selenium.webdriver.chrome.options import Options
>>> chrome_options = Options()
>>> chrome_options.add_argument("--headless")
>>> path_to_chromedriver = '/usr/local/bin/chromedriver'
>>> browser = webdriver.Chrome(path_to_chromedriver, chrome_options=chrome_options)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 140, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 229, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.10.0-32-generic x86_64)

到目前为止的步骤

修复崩溃

>>> from selenium import webdriver
>>> browser = webdriver.Chrome()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 62, in __init__
    self.service.start()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py", line 96, in start
    self.assert_process_still_running()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/common/service.py", line 109, in assert_process_still_running
    % (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

当我试图找到chromedriver的版本时

chromedriver --version

我收到了找不到共享库libnss3的错误。安装libnss3后,它有效。

所以行

RUN apt-get install libnss3

应该添加到Dockerfile。

修复'未找到chrome binary'

安装Chrome。仅通过pip安装chromedriver_installer是不够的。

RUN apt-get install wget
RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install

1 个答案:

答案 0 :(得分:1)

当标准图像已经可用时,您正尝试创建自定义图像。您应该使用selenium/chrome-standalone

docker run -d -p 4444:4444 selenium/standalone-chrom

然后在您的代码中启动RemoteWebdriver

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(desired_capabilities=DesiredCapabilities.CHROME)