如何修复Docker容器中的“ org.openqa.selenium.WebDriverException:未知错误:Chrome无法启动:崩溃”

时间:2019-07-11 15:10:39

标签: selenium docker google-chrome selenium-webdriver gradle

我正在尝试为硒测试构建一个docker映像。但是,我一直收到错误消息“ org.openqa.selenium.WebDriverException:未知错误:Chrome无法启动:崩溃”。

请不要将其标记为“重复”,尽管我在下面的链接中提到了很多答案。我仍然无法解决这个问题。我已经尝试了所有提供的答案,但是没有运气。 Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed

WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser

请找到docker文件代码和我的硒代码。

Docker文件代码如下:

FROM selenium/standalone-chrome
  FROM gradle
  RUN gradle wrapper
  USER root
  RUN apt-get update; apt-get -y install wget gnupg2
  RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | 
  apt-key add -
  RUN echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable 
  main" >> /etc/apt/sources.list.d/google-chrome.list
  RUN apt-get update; apt-get -y install google-chrome-stable
  COPY . /project
  RUN chown -R gradle:gradle /project
  RUN wget -N  
http://chromedriver.storage.googleapis.com/76.0.3809.25/chromedriver_linux64.zip -P ~/
  RUN unzip ~/chromedriver_linux64.zip -d ~/
  RUN rm ~/chromedriver_linux64.zip
  RUN mv -f ~/chromedriver /project/executables/chromedriver
  RUN chown gradle:gradle /project/executables/chromedriver
  RUN chmod 0755 /project/executables/chromedriver
  USER gradle
  WORKDIR /project
  ENV GRADLE_USER_HOME /project/.gradle_home
  CMD gradle build --info

硒代码:

 ChromeOptions chromeOptions = new ChromeOptions();
   chromeOptions.addArguments("--headless");
   chromeOptions.addArguments("start-maximized"); // open Browser in maximized mode
   chromeOptions.addArguments("disable-infobars"); // disabling infobars
   chromeOptions.addArguments("--disable-extensions"); // disabling extensions
  chromeOptions.addArguments("--disable-gpu"); // applicable to windows os only
  chromeOptions.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
  chromeOptions.addArguments("--no-sandbox"); // Bypass OS security model


  System.setProperty("webdriver.chrome.driver","executables/chromedriver");
  Webdriver driver = new ChromeDriver(chromeOptions);
  driver.get("http://google.com");

从错误消息中可以看到,Chrome浏览器从默认位置(usr / bin / google-chrome)开始运行,但是崩溃了。

Starting ChromeDriver 76.0.3809.25 (a0c95f440512e06df1c9c206f2d79cc20be18bb1-refs/branch-heads/3809@{#271}) on port 30275
    Only local connections are allowed.

" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed" . 
 (unknown error: DevToolsActivePort file doesn't exist)
 (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
System info: host: 'd2e61fa0170d', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.9.125-linuxkit', java.version: '1.8.0_212'
  Driver info: driver.version: ChromeDriver

我正在使用最新的Chrome驱动程序76.0.3809.25。我假设已获取并安装了最新的Google chrome

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

似乎您在安装google chrome及其驱动程序时遇到问题。与您共享我的Dockerfile和Docker-compose.yml。我使用python实现了这一点。它还具有Firefox和PhantomJS的示例。

FROM ubuntu:bionic

RUN apt-get update && apt-get install -y \
    python3 python3-pip \
    fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 \
    libnspr4 libnss3 lsb-release xdg-utils libxss1 libdbus-glib-1-2 \
    curl unzip wget \
    xvfb


# install geckodriver and firefox

RUN GECKODRIVER_VERSION=`curl https://github.com/mozilla/geckodriver/releases/latest | grep -Po 'v[0-9]+.[0-9]+.[0-9]+'` && \
    wget https://github.com/mozilla/geckodriver/releases/download/$GECKODRIVER_VERSION/geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz && \
    tar -zxf geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz -C /usr/local/bin && \
    chmod +x /usr/local/bin/geckodriver && \
    rm geckodriver-$GECKODRIVER_VERSION-linux64.tar.gz

RUN FIREFOX_SETUP=firefox-setup.tar.bz2 && \
    apt-get purge firefox && \
    wget -O $FIREFOX_SETUP "https://download.mozilla.org/?product=firefox-latest&os=linux64" && \
    tar xjf $FIREFOX_SETUP -C /opt/ && \
    ln -s /opt/firefox/firefox /usr/bin/firefox && \
    rm $FIREFOX_SETUP


# install chromedriver and google-chrome

RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
    wget https://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
    unzip chromedriver_linux64.zip -d /usr/bin && \
    chmod +x /usr/bin/chromedriver && \
    rm chromedriver_linux64.zip

RUN CHROME_SETUP=google-chrome.deb && \
    wget -O $CHROME_SETUP "https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb" && \
    dpkg -i $CHROME_SETUP && \
    apt-get install -y -f && \
    rm $CHROME_SETUP


# install phantomjs

RUN wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2 && \
    tar -jxf phantomjs-2.1.1-linux-x86_64.tar.bz2 && \
    cp phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs && \
    rm phantomjs-2.1.1-linux-x86_64.tar.bz2


RUN pip3 install selenium
RUN pip3 install pyvirtualdisplay
RUN pip3 install Selenium-Screenshot
RUN pip3 install requests
RUN pip3 install pytest


ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONUNBUFFERED=1

ENV APP_HOME /usr/src/app
WORKDIR /$APP_HOME

COPY . $APP_HOME/

CMD tail -f /dev/null
CMD python3 example.py

Docker-compose.yml

selenium:
    build: .
    ports:
        - 4000:4000
        - 443:443
    volumes:
        - ./data/:/data/
    privileged: true