AWS Batch中Docker容器ChromeDriver与Chrome版本不匹配问题求助
问题:ChromeDriver与Chrome版本不匹配导致SessionNotCreatedException
此前在AWS Batch环境的Docker容器中运行镜像一直正常,今日突然出现以下错误:
E selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 114 E Current browser version is 116.0.5845.96 with binary path /opt/google/chrome/google-chrome
以下是安装Chrome的Dockerfile内容:
FROM python:3.10 WORKDIR /usr/src/app COPY . . RUN pip install --trusted-host pypi.org --upgrade pip RUN pip install --no-cache-dir \ --extra-index-url https://artifactory.int.csgdev01.citcosvc.com/artifactory/api/pypi/citco-pypi/simple \ -r requirements.txt RUN pip install awscli RUN apt-get install -yqq unzip curl RUN apt-get -y update RUN apt-get install zip -y RUN apt-get install unzip -y RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - RUN echo "deb 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 -y install -y google-chrome-stable # Install chrome driver RUN wget -N https://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip -P ~/ RUN unzip ~/chromedriver_linux64.zip -d ~/ RUN rm ~/chromedriver_linux64.zip RUN mv -f ~/chromedriver /usr/local/bin/chromedriver RUN chmod 0755 /usr/local/bin/chromedriver RUN ls -lt RUN ls -lt /usr/local/bin RUN chmod +x ./run.sh CMD ["bash", "./run.sh"]
Selenium Python测试类代码:
from selenium import webdriver import unittest class Test_SecTransferWorkflow(unittest.TestCase): options = webdriver.ChromeOptions() options.add_argument('--no-sandbox') options.add_argument("--enable-javascript") options.add_argument("--start-maximized") options.add_argument("--incognito") options.add_argument('--headless') options.add_argument('--ignore-certificate-errors') options.add_argument('--enable-features=NetworkService') options.add_argument('--shm-size=1g') options.add_argument('--disable-gpu') options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_argument("--window-size=1920,1080") options.add_argument("--disable-extensions") options.add_argument('--disable-dev-shm-usage') options.add_experimental_option('useAutomationExtension', False) options.add_experimental_option("detach", True) options.add_argument('--allow-running-insecure-content') options.add_argument('--allow-insecure-localhost') options.add_argument('--ignore-ssl-errors=yes') options.add_argument('--user-agent=Chrome/77') driver = webdriver.Chrome(options=options) @classmethod def setUpClass(cls): try: cls.driver.delete_all_cookies() cls.driver.get(TestData_common.BASE_URL) time.sleep(2) except WebDriverException as e: print('Site down...> ', e) cls.driver.delete_all_cookies() time.sleep(3) def test_001_login(self): if not TestData_common.URL_FOUND: pytest.skip('Site seems to be down...') self.loginPage = LoginPage(self.driver) self.loginPage.do_click_agree_button() self.driver.maximize_window() print('Successfully clicked on AGREE button...') time.sleep(2)
解决方案
原因分析
Dockerfile中安装google-chrome-stable会自动拉取最新版本(当前为116),但安装ChromeDriver时拉取的是构建镜像时的最新稳定版(114),导致版本不匹配,触发SessionNotCreatedException。
方案1:固定Chrome版本安装,避免自动更新
修改Dockerfile中安装Chrome的步骤,指定具体版本(比如114.0.5735.198),确保和ChromeDriver版本一致:
# 替换原安装google-chrome-stable的命令 RUN apt-get -y install -y google-chrome-stable=114.0.5735.198-1 # 锁定版本防止后续更新 RUN apt-mark hold google-chrome-stable
同时,安装ChromeDriver时也固定对应版本,避免拉取最新版:
# 替换原ChromeDriver安装命令 RUN wget -N https://chromedriver.storage.googleapis.com/114.0.5735.90/chromedriver_linux64.zip -P ~/
方案2:使用webdriver-manager自动匹配版本
- 在requirements.txt中添加
webdriver-manager - 修改Python代码,用webdriver-manager自动获取对应版本的ChromeDriver:
from selenium import webdriver from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager import unittest class Test_SecTransferWorkflow(unittest.TestCase): options = webdriver.ChromeOptions() # 保留原所有options配置 options.add_argument('--user-agent=Chrome/77') # 替换原driver初始化代码 driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) # 后续方法保留不变
这种方式无需手动维护ChromeDriver版本,会自动匹配当前安装的Chrome版本。
方案3:手动安装对应116版本的ChromeDriver
直接在Dockerfile中安装Chrome 116对应的ChromeDriver(版本号116.0.5845.96):
# 替换原ChromeDriver安装命令 RUN wget -N https://chromedriver.storage.googleapis.com/116.0.5845.96/chromedriver_linux64.zip -P ~/
内容的提问来源于stack exchange,提问作者Ram
相关产品推荐
相关产品推荐

