bumbummath
bumbummath

Reputation: 31

Error while running Selenium with Chrome in Docker

I am attempting to deploy a Python script in a docker container that utilizes Selenium ChromeWebDriver:

def initialize_driver(self):
        try:
            options = webdriver.ChromeOptions()
            options.add_argument("--headless")
            options.add_argument("--no-sandbox")
            options.add_argument("--disable-dev-shm-usage")
            driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),  options=options)
            return driver
        except Exception as error:
            logging.error("INITIAL:",error)

However, i encountered the following error message:

Logging error ---

Traceback (most recent call last):

File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/file_manager.py", line 65, in __extract_zip

archive.extractall(to_directory)

File "/usr/local/lib/python3.11/zipfile.py", line 1677, in extractall

self._extract_member(zipinfo, path, pwd)

File "/usr/local/lib/python3.11/zipfile.py", line 1732, in _extract_member

shutil.copyfileobj(source, target)

File "/usr/local/lib/python3.11/shutil.py", line 197, in copyfileobj

buf = fsrc_read(length)

^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/zipfile.py", line 951, in read

data = self._read1(n)

^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/zipfile.py", line 1019, in _read1

data += self._read2(n - len(data))

^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/zipfile.py", line 1054, in _read2

raise EOFError

EOFError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/app/newsarticle.py", line 122, in initialize_driver

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/site-packages/webdriver_manager/chrome.py", line 40, in install

driver_path = self._get_driver_binary_path(self.driver)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/manager.py", line 41, in _get_driver_binary_path

binary_path = self._cache_manager.save_file_to_cache(driver, file)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/driver_cache.py", line 54, in save_file_to_cache

files = self.unpack_archive(archive, path)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/driver_cache.py", line 49, in unpack_archive

return self._file_manager.unpack_archive(archive, path)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/file_manager.py", line 57, in unpack_archive

return self.__extract_zip(archive_file, target_dir)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/site-packages/webdriver_manager/core/file_manager.py", line 67, in __extract_zip

if e.args[0] not in [26, 13] and e.args[1] not in [


IndexError: tuple index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/usr/local/lib/python3.11/logging/__init__.py", line 1110, in emit

msg = self.format(record)

^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/logging/__init__.py", line 953, in format

return fmt.format(record)

^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/logging/__init__.py", line 687, in format

record.message = record.getMessage()

^^^^^^^^^^^^^^^^^^^

File "/usr/local/lib/python3.11/logging/__init__.py", line 377, in getMessage

msg = msg % self.args

~~~~^~~~~~~~~~~

TypeError: not all arguments converted during string formatting

Call stack:

File "/usr/local/lib/python3.11/threading.py", line 995, in _bootstrap

self._bootstrap_inner()

File "/usr/local/lib/python3.11/threading.py", line 1038, in _bootstrap_inner

This is the Dockerfile:

FROM python:3.11.2
WORKDIR /app
ADD . /app 
RUN apt -f install
RUN apt-get update && apt-get install -y curl && apt-get install -y wget && apt-get install bzip2 -y && apt-get install unzip -y

RUN wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
RUN dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install

COPY requirements.txt requirements.txt 
RUN pip install -r ./requirements.txt 
CMD [ "python", "main.py" ]

My requirements.txt:

selenium
packaging
webdriver_manager
chromedriver-binary```

I tried to add

RUN apt-get install -y gconf-service libasound2 libatk1.0-0 libcairo2 libcups2 libfontconfig1 libgdk-pixbuf2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libxss1 fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils

to the Dockerfile but that didn't change anything. Any help is much appreciated

Upvotes: 1

Views: 263

Answers (1)

bumbummath
bumbummath

Reputation: 31

I solved it. The problem was that i downloaded the wrong version of the chromedriver in the dockerfile.

Upvotes: 0

Related Questions