4

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)

3
  • docker run imagename /opt/example1/assets/chromedriver might be informative; so far the error message you've shown just says that doesn't work. Commented Jan 4, 2019 at 17:15
  • @DavidMaze Here is my output: (carcrawler) Lukas-MacBook-Pro-2:carcrawler lukadragicevic$ docker run lukatest /opt/example1/assets/chromedriver /opt/example1/assets/chromedriver: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory Commented Jan 4, 2019 at 18:52
  • @DavidMaze I have updated my post (see Edit 1). Thanks for the response. Commented Jan 4, 2019 at 19:40

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have exactly the same code now and it is running well, but I got the message from target website (mobile.de) "Unfortunately, automated access to this page was denied.". How could I pass this block?
1

Although this is an older question, I tried several fixes found across the various stackoverflow boards and none worked

Chrome driver requires additional libraries. I was able to solve the issue with a single command on Linux:

sudo apt-get install chromium-driver

Many libraries and dependencies were all installed and finally the Chrome webdriver operated as expected within my code.

Comments

0

Did you not forget to add headless mode ?

chrome_options = Options()
chrome_options.add_argument("--headless")

1 Comment

I forgot, now I have tested with this part of the code, but I faced same error.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.