Docker Selenium: selenium.common.exceptions.WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 127

Question:

I am using selenium’s chromedriver in my python project.

I am building successfully my Dockerfile:

FROM ubuntu:17.04
FROM selenium/standalone-chrome
FROM python:3.6
RUN apt update
RUN apt-get install -y libnss3 libgconf-2-4
ADD ./requirements.txt /tmp/requirements.txt
RUN python -m pip install -r /tmp/requirements.txt
ADD . /opt/example1/
# rights?
RUN chmod +x /opt/example1/assets/chromedriver
WORKDIR /opt/example1
CMD ["python","-u","program.py"]

But when I run my docker container I got following error:

Traceback (most recent call last): File “program.py”, line 8, in

MdCrawler(MD_START_URL, “MobileDe”).start() File “/opt/example1/mobile_de_crawler.py”, line 17, in init
self.web_driver_chrome = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH) File
“/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py”,
line 73, in init
self.service.start() File “/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py”,
line 98, in start
self.assert_process_still_running() File “/usr/local/lib/python3.6/site-packages/selenium/webdriver/common/service.py”,
line 111, in assert_process_still_running
% (self.path, return_code) selenium.common.exceptions.WebDriverException: Message: Service
/opt/example1/assets/chromedriver unexpectedly exited. Status code
was: 127

Anyone got an idea what could I do to prevent this error?
What is causing this crash?

Here is my initialization code where error occurs:

CHROME_DRIVER_PATH = os.path.abspath('assets/chromedriver')


class MdCrawler(Crawler):

def __init__(self, start_url, source):
    super().__init__(start_url, source)
    serialized_arr = self.read_data_from_json_file(JSON_FILE_PATH)
    self.sent_ids = [] if serialized_arr is None else serialized_arr
    >>> self.web_driver_chrome = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
    exit(1)

Edit 1:

I have edited Dockerfile (added ubuntu:17.04 and aptget libnss3 libgconf-2-4). After building my docker image, I got different error:

selenium.common.exceptions.WebDriverException: Message: unknown error:
cannot find Chrome binary (Driver info: chromedriver=2.45.615279
(12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux
4.9.125-linuxkit x86_64)

Edit 2:

I have added

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' | tee /etc/apt/sources.list.d/google-chrome.list
RUN apt-get update
RUN apt-get install -y google-chrome-stable

To my Dockerfile, but new error is coming:

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error:
Chrome failed to start: exited abnormally (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.) (Driver info:
chromedriver=2.45.615279
(12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux
4.9.125-linuxkit x86_64)

Asked By: Lululu

||

Answers:

Did you not forget to add headless mode ?

chrome_options = Options()
chrome_options.add_argument("--headless")
Answered By: Maciej.O

My minimal test script for Selenium chromedriver inside my Docker container looks like this:

import selenium.webdriver

options = selenium.webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')

driver = selenium.webdriver.Chrome(chrome_options=options)
driver.get('https://www.python.org/')
print(driver.title)
driver.close()

So it looks like you’re missing --headless and --no-sandbox arguments.

Answered By: finefoot

For future reference:

In my case I had an if/else statement in my code that was setting the --headless parameter based on an environment variable, but this env variable was not properly exported and was therefore not accessible within my (C#) program.

–> Consequently Chrome was started without –headless inside my docker container, causing the DevToolsActivePort file doesn't exist error.

I now explicitly set this in the Dockerfile:
ENV Selenium_UseHeadlessDriver=true

Answered By: veuncent