1

I am trying to build a docker image for my selenium tests. However i keep getting error message " org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: crashed" .

Please do not mark this as Duplicate ,though I have referred to a lot of answers provided in the links below. I am still not able to get through this. I have tried all the answers that are provided but no luck . 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

Please find the docker file code and my selenium code.

Docker file code looks like this :

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

Selenium code :

 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");

As you can see from the error message the chrome is starting at default location(usr/bin/google-chrome) but it is crashing .

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

I am using latest chrome driver 76.0.3809.25. I am assuming that latest google chrome is fetched and installed

Any help is appreciated

Deva Reddy
  • 19
  • 3
  • The first Chrome run will setup certain dirs/configs... so you may just need to run it once and then close it before running your "new ChromeDriver" command. – pcalkins Jul 15 '19 at 23:36
  • can you please elaborate – Deva Reddy Jul 17 '19 at 13:23
  • I've noticed that Chrome will crash if the first run is done via Selenium call... so I thought that might be something to check. Though I did get this particular exception last night and that wasn't the case, so it might be something else. (at the time I was running multiple instances of Chrome one after the other but I haven't had a chance to troubleshoot this yet.) – pcalkins Jul 17 '19 at 16:51
  • I have a feeling it's a timing issue. When Selenium launches Chrome it'll create a temp folder for the temporary profile. It's possible this part is failing because it's run during a cleanup of another instance. – pcalkins Jul 17 '19 at 18:24
  • someone had mentioned that the "--no-sandbox" option has to be the first line. I'm wondering if you had tried that... (I haven't yet as I don't have a repeatable scenario...) – pcalkins Jul 17 '19 at 18:31

1 Answers1

1

Seems like you are having issues with installing google chrome and its driver. Sharing you my Dockerfile and Docker-compose.yml. I achieved this using python. It also has the example for Firefox and 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
Bilal Ali Jafri
  • 915
  • 1
  • 6
  • 17