Docker中运行Selenium ChromeWebDriver报错,寻求解决方案
Docker容器部署Selenium ChromeWebDriver脚本报错解决
初始化Driver代码
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)
报错信息
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
当前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" ]
requirements.txt内容
selenium packaging webdriver_manager chromedriver-binary
已尝试操作
曾在Dockerfile中添加以下依赖,但问题未解决:
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
解决方案
1. 修复日志格式化错误
报错中的TypeError源于日志语句格式错误,修改代码中的日志行:
# 原代码 logging.error("INITIAL:",error) # 修改为以下任意一种 logging.error("INITIAL: %s", error) # 或者 logging.error(f"INITIAL: {error}")
2. 手动安装对应版本的ChromeDriver,避免自动下载问题
容器中已安装Chrome,直接手动安装匹配版本的ChromeDriver,绕过webdriver-manager的自动下载逻辑(EOFError是下载的压缩包损坏导致)。
修改Dockerfile,添加ChromeDriver安装步骤
FROM python:3.11.2 WORKDIR /app # 合并安装命令,清理缓存减小镜像 RUN apt-get update && apt-get install -y --no-install-recommends \ curl wget unzip \ && wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \ && dpkg -i google-chrome-stable_current_amd64.deb \ && apt-get -fy install \ && rm -rf /var/lib/apt/lists/* google-chrome-stable_current_amd64.deb # 获取Chrome版本并安装对应ChromeDriver RUN CHROME_VERSION=$(google-chrome --version | grep -oP '\d+\.\d+\.\d+\.\d+') && \ wget -O /tmp/chromedriver.zip https://storage.googleapis.com/chrome-for-testing-public/$CHROME_VERSION/linux64/chromedriver-linux64.zip && \ unzip /tmp/chromedriver.zip -d /usr/local/bin/ && \ chmod +x /usr/local/bin/chromedriver-linux64/chromedriver && \ ln -s /usr/local/bin/chromedriver-linux64/chromedriver /usr/local/bin/chromedriver && \ rm /tmp/chromedriver.zip COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD ["python", "main.py"]
修改Python初始化代码,指定ChromeDriver路径
def initialize_driver(self): try: options = webdriver.ChromeOptions() options.add_argument("--headless") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") # 直接指定本地ChromeDriver路径 driver = webdriver.Chrome(service=Service("/usr/local/bin/chromedriver"), options=options) return driver except Exception as error: logging.error("INITIAL: %s", error)
3. 升级webdriver-manager版本
报错中的IndexError是webdriver-manager的已知bug,升级到最新版本可修复:
修改requirements.txt:
selenium packaging webdriver-manager>=4.0.1 chromedriver-binary
然后重新构建容器。
内容的提问来源于stack exchange,提问作者bumbummath
相关产品推荐
相关产品推荐

