【问题标题】:headless chrome in docker with python. Chrome failed to start: crashed带有 python 的 docker 中的无头 chrome。 Chrome 无法启动:崩溃
【发布时间】:2017-02-10 05:40:35
【问题描述】:

我想在 docker 容器中运行这个简单的脚本:

def hi_chrome():
    from xvfbwrapper import Xvfb
    from splinter import Browser
    vdisplay = Xvfb()
    vdisplay.start()

    print "spawning connector"
    oBrowser = Browser('chrome')
    oBrowser.visit("http://google.co.za")
    assert oBrowser.title == "Google"
    print "yay"
    vdisplay.stop()

if __name__ == '__main__':
    hi_chrome()

通过执行我的 docker 文件中列出的所有 pip 和 apt-get 安装并运行脚本,我已经让脚本在虚拟环境中运行。但是当我尝试在容器中运行它时,我得到:

Traceback (most recent call last):
  File "app.py", line 19, in <module>
    hi_chrome()
  File "app.py", line 10, in hi_chrome
    oBrowser = Browser('chrome')
  File "/usr/local/lib/python2.7/dist-packages/splinter/browser.py", line 63, in Browser
    return driver(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/splinter/driver/webdriver/chrome.py", line 31, in __init__
    self.driver = Chrome(chrome_options=options, **kwargs)
  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 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
    response = self.execute(Command.NEW_SESSION, capabilities)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, 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.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.8.0-34-generic x86_64)

在尝试使用 docker-hub 上的其他容器运行我的脚本时,我遇到了类似的问题。我尝试过使用 chrome 代替 chromium,并且尝试过使用在 docker-hub 上找到的一些容器,但我一直在寻找破碎的废话。这应该很简单。

我的主要怀疑是它是版本控制的事情。但它在 venv 中有效,因此没有太大意义。或者 docker 只是需要一些花哨的东西来让 chrome webdriver 运行。

有人能指出我明显的愚蠢错误吗?

我的 Dockerfile 看起来像

FROM ubuntu:16.04

RUN apt-get update -y && \
    apt-get install -y python-pip python-dev xvfb chromium-browser && \
    pip install --upgrade pip setuptools

RUN pip install chromedriver_installer

COPY ./requirements.txt /app/requirements.txt

WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

ENTRYPOINT [ "python" ]

CMD [ "app.py" ]

还有 requirements.txt:

splinter==0.7.5
xvfbwrapper==0.2.8

【问题讨论】:

    标签: python docker selenium-chromedriver


    【解决方案1】:

    我找到了一个可以正常工作的图像,然后将其击败提交......这个解决方案的好处是它不需要xvfbwrapper,所以它很好而且简单。

    应用程序.py

    def hi_chrome():
        # from xvfbwrapper import Xvfb
        from splinter import Browser
        # vdisplay = Xvfb()
        # vdisplay.start()
    
        print "spawning connector"
        oBrowser = Browser('chrome')
        oBrowser.visit("http://google.co.za")
        assert oBrowser.title == "Google"
        print "yay"
    
        # vdisplay.stop()
    
    if __name__ == '__main__':
        hi_chrome()
    

    要求:

     splinter==0.7.5
    

    Dockerfile

    FROM markadams/chromium-xvfb
    
    RUN apt-get update && apt-get install -y \
        python python-pip curl unzip libgconf-2-4
    
    ENV CHROMEDRIVER_VERSION 2.26
    
    RUN curl -SLO "https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip" \
      && unzip "chromedriver_linux64.zip" -d /usr/local/bin \
      && rm "chromedriver_linux64.zip"
    
    COPY requirements.txt /usr/src/app/requirements.txt
    WORKDIR /usr/src/app
    
    RUN pip install -r requirements.txt
    
    COPY . /usr/src/app
    
    ENTRYPOINT [ "python" ]
    
    CMD [ "app.py" ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多