会话未创建:此版本的 ChromeDriver 仅支持 Chrome 版本 114

Ram*_*Ram 15 python google-chrome amazon-web-services selenium-webdriver docker

我正在 AWS Batch 环境中的 Docker 容器中运行 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
Run Code Online (Sandbox Code Playgroud)

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" >> 
Run Code Online (Sandbox Code Playgroud)

/etc/apt/sources.list.d/google-chrome.list 运行 apt-get -y update 运行 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"]
Run Code Online (Sandbox Code Playgroud)

我的 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)
Run Code Online (Sandbox Code Playgroud)

到目前为止,我在运行此映像时没有遇到任何问题,直到今天遇到此错误。任何帮助深表感谢。

syt*_*ech 5

chrome (116) 和 chromedriver (114) 的版本不匹配。这是因为最新版本的 chromedriver(如 所描述的chromedriver.storage.googleapis.com/LATEST_RELEASE)不一定总是与 debian 存储库中的最新版本的 Chrome 相匹配。尽管这些主要版本通常会匹配(这就是为什么这在过去对您有用),但您不能始终依赖这种情况,正如您现在所看到的。

相反,您应该检查 chrome 的版本,然后安装适当版本的 chromedriver。正如chromedriver 下载页面上所述,您可以使用其API 端点来查找各种版本 chromedriver 的下载链接,或者查找仪表板上的链接,这两个链接都将包含下载与 chrome 116 兼容的 chromedriver 版本的链接 -例如,在撰写本文时:(https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/116.0.5845.96/linux64/chromedriver-linux64.zip但请注意,此 zip 结构可能与您已经使用的下载链接不同)。

在我自己的 dockerfile 中,我手动指定 chromedriver 下载 URL,然后运行脚本来测试主要版本是否匹配。不过,您可以使用上面提到的 API 端点来自动获取正确的 chromedriver URL。


至于为什么chromedriver.storage.googleapis.com/LATEST_RELEASE指向主要版本 114 而不是 116,尽管可以下载 116 并且稳定的 debian 版本是 116,说实话,我不太确定。