3
votes

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)

2
docker run imagename /opt/example1/assets/chromedriver might be informative; so far the error message you've shown just says that doesn't work.David Maze
@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 directoryLululu
@DavidMaze I have updated my post (see Edit 1). Thanks for the response.Lululu

2 Answers

0
votes

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.

0
votes

Did you not forget to add headless mode ?

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