Docker容器无法读取WebDriver文件问题求助
解决Docker容器中Selenium的NoSuchDriverException错误
问题详情
运行Docker容器时触发NoSuchDriverException,提示无法定位Chrome WebDriver。即使Dockerfile中已将WebDriver文件复制到容器工作目录,问题仍未解决。
错误栈
Traceback (most recent call last): File "/app/scraper_exe.py", line 3, in <module> bot = Scraper() ^^^^^^^^^ File "/app/scraper.py", line 25, in __init__ self.driver = webdriver.Chrome(service=service, options=options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/chrome/webdriver.py", line 45, in __init__ super().__init__( File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/chromium/webdriver.py", line 51, in __init__ self.service.path = DriverFinder.get_path(self.service, options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/selenium/webdriver/common/driver_finder.py", line 44, in get_path raise NoSuchDriverException(f"Unable to locate or obtain driver for {options.capabilities['browserName']}") selenium.common.exceptions.NoSuchDriverException: Message: Unable to locate or obtain driver for chrome; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location
Dockerfile内容
FROM python:3.11.4-bookworm WORKDIR /app COPY . /app/ RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' RUN apt-get -y update RUN apt-get install -y google-chrome-stable RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip RUN apt-get install -yqq unzip RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ RUN pip3 install -r requirements.txt CMD ["python", "scraper_exe.py"]
requirements.txt内容
selenium pandas urllib3 boto3 click
本地目录结构
$ ls __pycache__/ chromedriver.exe* data_pipeline_scraper.ipynb Dockerfile requirements.txt scraper.py scraper_exe.py
解决方案
1. 显式指定ChromeDriver路径
在初始化Chrome驱动时,直接指定容器内已安装的ChromeDriver路径,避免Selenium自动查找失败:
from selenium import webdriver from selenium.webdriver.chrome.service import Service # 显式指定chromedriver路径 service = Service(executable_path='/usr/local/bin/chromedriver') options = webdriver.ChromeOptions() # 添加无头模式等必要配置 options.add_argument('--headless=new') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') self.driver = webdriver.Chrome(service=service, options=options)
2. 确保ChromeDriver具备可执行权限
在Dockerfile中添加权限设置命令,保证ChromeDriver能被正常执行:
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ # 新增权限配置 RUN chmod +x /usr/local/bin/chromedriver
3. 改用Selenium Manager自动管理驱动(推荐)
Selenium 4.6.0及以上版本自带Selenium Manager,可自动匹配并下载对应版本的ChromeDriver,无需手动处理驱动文件。简化后的Dockerfile如下:
FROM python:3.11.4-bookworm WORKDIR /app COPY . /app/ # 安装Chrome浏览器 RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' RUN apt-get update && apt-get install -y google-chrome-stable RUN pip3 install -r requirements.txt CMD ["python", "scraper_exe.py"]
此时代码无需手动指定驱动路径,Selenium会自动处理:
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--headless=new') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') self.driver = webdriver.Chrome(options=options)
4. 验证Chrome与ChromeDriver版本匹配
若坚持手动下载驱动,需确保Chrome版本与ChromeDriver版本完全一致:
- 查看容器内Chrome版本:执行
google-chrome --version - 替换Dockerfile中的下载命令,指定对应版本的ChromeDriver:
# 示例:Chrome版本为120.0.6099.109时,直接指定版本号下载 RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/120.0.6099.109/chromedriver_linux64.zip
内容的提问来源于stack exchange,提问作者Corbie
相关产品推荐
相关产品推荐

