在Docker中为Chrome安装匹配版本的ChromeDriver
问题
我的Selenium爬虫此前在AWS ECS上运行正常,近期出现如下错误:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 114
Current browser version is 119.0.6045.199 with binary path /usr/bin/google-chrome
我知道需要让Chrome与ChromeDriver版本匹配,但不擅长编写命令行脚本,不知道怎么解决。当前我的相关文件内容如下:
Dockerfile
from python:3.10.11-bullseye # install google 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 -y update --fix-missing RUN apt-get install -y google-chrome-stable # install chromedriver RUN apt-get install -yqq unzip RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ # install requirements RUN mkdir /app ADD . /app WORKDIR /app RUN pip install -r requirements.txt ENTRYPOINT ["python", "app.py"]
app.py(顶部内容)
# import relevant selenium packages from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.action_chains import ActionChains # set up headless option chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--allow-running-insecure-content') chrome_options.add_argument('--ignore-certificate-errors') # import support packages from datetime import date from datetime import datetime import time import re import logging import numpy as np import pandas as pd import traceback # import AWS-related packages import boto3 from io import StringIO # initiate the Selenium driver driver = webdriver.Chrome(chrome_options=chrome_options)
requirements.txt
# packages selenium==4.8.2 boto3 numpy==1.23.2 pandas==1.4.3
解决方案
方案一:固定Chrome与ChromeDriver版本
直接指定二者的匹配版本,避免自动更新导致版本不一致。修改后的Dockerfile如下:
from python:3.10.11-bullseye # 安装指定版本的Google 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 -y update --fix-missing # 指定版本安装Chrome RUN apt-get install -y google-chrome-stable=119.0.6045.199-1 # 安装对应版本的ChromeDriver RUN apt-get install -yqq unzip # 下载与Chrome匹配的ChromeDriver版本 RUN wget -O /tmp/chromedriver.zip https://storage.googleapis.com/chromedriver/releases/119.0.6045.199/chromedriver_linux64.zip RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ # 赋予ChromeDriver执行权限 RUN chmod +x /usr/local/bin/chromedriver # install requirements RUN mkdir /app ADD . /app WORKDIR /app RUN pip install -r requirements.txt ENTRYPOINT ["python", "app.py"]
说明:
- 安装Chrome时指定
119.0.6045.199-1版本,确保和当前浏览器版本完全匹配 - ChromeDriver版本号与Chrome主版本保持一致,直接下载对应版本即可
方案二:使用Selenium自动驱动管理(更省心)
Selenium 4.6+版本自带驱动管理工具,可自动下载匹配当前Chrome版本的ChromeDriver,无需手动维护版本。
步骤1:更新requirements.txt中的Selenium版本
# packages selenium==4.15.0 # 升级到4.6以上的稳定版 boto3 numpy==1.23.2 pandas==1.4.3
步骤2:修改app.py的驱动初始化代码
# import relevant selenium packages from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import NoSuchElementException from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.action_chains import ActionChains # 新增导入驱动管理相关模块 from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager # set up headless option chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('--headless') chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--allow-running-insecure-content') chrome_options.add_argument('--ignore-certificate-errors') # 指定Chrome二进制文件路径(确保程序能找到Chrome) chrome_options.binary_location = "/usr/bin/google-chrome" # import support packages from datetime import date from datetime import datetime import time import re import logging import numpy as np import pandas as pd import traceback # import AWS-related packages import boto3 from io import StringIO # 修改驱动初始化逻辑,用自动管理工具匹配版本 driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
步骤3:简化Dockerfile(可选)
无需手动安装ChromeDriver,可删除相关步骤:
from python:3.10.11-bullseye # install google 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 -y update --fix-missing RUN apt-get install -y google-chrome-stable # install requirements RUN mkdir /app ADD . /app WORKDIR /app RUN pip install -r requirements.txt ENTRYPOINT ["python", "app.py"]
说明:
- 该方式会在每次启动时自动检测Chrome版本,下载对应ChromeDriver,彻底避免版本不匹配问题,适合不想手动维护版本的场景
额外注意事项
- 方案一后续若Chrome更新,需手动修改Dockerfile中的版本号;方案二则无需手动干预
- 修改完成后,在AWS ECS上重新构建Docker镜像并部署即可生效
内容的提问来源于stack exchange,提问作者Life is Good
相关产品推荐
相关产品推荐

