'Chromedriver executable path not found in Docker Container

I have created a docker image with the Docker file below. It installs the latest versions of Google Chrome and the chrome driver. As well as the other pip packages.

Dockerfile

FROM python:3.9

# Install Chrome WebDriver
RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
    mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
    unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    rm /tmp/chromedriver_linux64.zip && \
    chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
    ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver

# Install Google Chrome
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
    echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
    apt-get -yqq update && \
    apt-get -yqq install google-chrome-stable && \
    rm -rf /var/lib/apt/lists/*


COPY requirements.txt .

RUN pip install -r requirements.txt

WORKDIR /seltesting

COPY ./app ./app

CMD ["python", "./app/main.py"]

The chromedriver.exe file is in the container as I have found it in the CLI. It is in this directory '/usr/local/bin/chromedriver'.

python code

driver = webdriver.Chrome(options=options, executable_path='/usr/local/bin/chromedriver')

I am using a venv as I am also using flask to create a micro service that uses the chrome driver. Would that be causing an issue?

Any assist would be much appreciated as I have been stuck on this for a long time.



Solution 1:[1]

I have found the problem, you need to add the all the python files into the Dockerfile. Please find the Dockerfile to install the Chromedriver and Chrome onto the image and the default path for the chromedriver within the container.

Dockerfile

FROM python:3.9

ADD /app/main.py .
ADD /app/connectdriver.py .

# Install Chrome WebDriver
RUN CHROMEDRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE` && \
    mkdir -p /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    curl -sS -o /tmp/chromedriver_linux64.zip http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip && \
    unzip -qq /tmp/chromedriver_linux64.zip -d /opt/chromedriver-$CHROMEDRIVER_VERSION && \
    rm /tmp/chromedriver_linux64.zip && \
    chmod +x /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver && \
    ln -fs /opt/chromedriver-$CHROMEDRIVER_VERSION/chromedriver /usr/local/bin/chromedriver

# Install Google Chrome
RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
    echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list && \
    apt-get -yqq update && \
    apt-get -yqq install google-chrome-stable && \
    rm -rf /var/lib/apt/lists/*

COPY requirements.txt .

RUN pip install -r requirements.txt

WORKDIR /seleniumtesting

COPY ./app ./app


CMD ["python","./app/main.py"]

python driver script

driver = webdriver.Chrome(options=options, executable_path='/usr/local/bin/chromedriver')

Solution 2:[2]

In Python-Selenium I wouldn't pass the chromedriver path, instead I will use auto installer, so that it won't fail in such cases.

chromedriver-autoinstaller

Automatically download and install chromedriver that supports the currently installed version of chrome. This installer supports Linux, MacOS and Windows operating systems.

Installation

pip install chromedriver-autoinstaller

Usage

Just type import chromedriver_autoinstaller in the module you want to use chromedriver.

Example

from selenium import webdriver
import chromedriver_autoinstaller


chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists
                                      # and if it doesn't exist, download it automatically,
                                      # then add chromedriver to path

driver = webdriver.Chrome()
driver.get("http://www.python.org")
assert "Python" in driver.title

Conclusion :

If you see above, I have not pass any path instead it is just, driver = webdriver.Chrome() preceded by chromedriver_autoinstaller.install(), should help you past the issue.

Official Reference link

Solution 3:[3]

Make sure that the path of chrome driver that you are providing is correct.

Then:

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome(executable_path = "PATH/to/chromedriver", options = chrome_options)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Calllum
Solution 2 cruisepandey
Solution 3 fam