10

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.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Callum
  • 231
  • 1
  • 3
  • 12
  • Are you able to start that executable manually in the container? – Alexey R. Sep 02 '21 at 10:00
  • Also can you share the exact text of the error you get? – Alexey R. Sep 02 '21 at 10:06
  • Hi @AlexeyR. thank you so much for your help when I execute the file in the container I get this message - Starting ChromeDriver 92.0.4515.107 (87a818b10553a07434ea9e2b6dccf3cbe7895134-refs/branch-heads/4515@{#1634}) on port 9515 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. [1630890753.041][SEVERE]: bind() failed: Cannot assign requested address (99) ChromeDriver was started successfully. – Callum Sep 06 '21 at 01:16
  • Try to apply this https://stackoverflow.com/questions/55844788/how-to-fix-severe-bind-failed-cannot-assign-requested-address-99-while – Alexey R. Sep 06 '21 at 06:16
  • @AlexeyR. Thank you for the assistance. I have just tried that solution and even though I do not get that error anymore. The chrome driver can still not be found. Starting ChromeDriver 92.0.4515.107 (87a818b10553a07434ea9e2b6dccf3cbe7895134-refs/branch-heads/4515@{#1634}) on port 9515 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully I am trying to run the chromedriver within a Flask application, would you know if this may be causing me issues? – Callum Sep 08 '21 at 23:11
  • What do you mean? The message you are showing means you have successfully started your driver – Alexey R. Sep 09 '21 at 05:57

2 Answers2

6

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')
Callum
  • 231
  • 1
  • 3
  • 12
1

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

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Hi @cruisepandey, I have tried this but unfortunately it did not work :(. I tried both ways of the configuration but no luck :( Thank you for you help though :). – Callum Sep 02 '21 at 05:50
  • Ideally it should have been, what went wrong with this technique ? – cruisepandey Sep 02 '21 at 05:50
  • it is still saying that the driver cannot be found. UnboundLocalError: local variable 'driver' referenced before assignment – Callum Sep 02 '21 at 06:04
  • Cause I think you missed to run `pip install chromedriver-autoinstaller` beforehand. Cause in my solution, you do not need to have a physical copy of `chromdriver.exe` – cruisepandey Sep 02 '21 at 06:05
  • 1
    sorry i did install the chromedriver-autoinstaller into the container and the requirements.txt page. As 'chromedriver-autoinstaller==0.2.2' – Callum Sep 02 '21 at 06:18
  • Not working with base image `python:3.10.7-bullseye`, Chrome binary are not found. – jlandercy Nov 03 '22 at 16:04