You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器中运行Selenium Python自动化测试时遭遇SessionNotCreatedException错误的排查求助

Docker容器中运行Selenium Python自动化测试时遭遇SessionNotCreatedException错误的排查求助

我用Python结合Selenium搭建了自动化测试框架,现在尝试把测试用例放到Docker容器里执行,但一直碰到SessionNotCreatedException报错。本地环境跑这些测试完全正常,一到容器里就失败了。

我试过参考AI工具的建议,设置了随机生成的临时用户数据目录:options.add_argument(f'--user-data-dir={user_data_dir}'),但问题依然存在。

下面是我用到的相关文件内容和完整的错误栈信息,麻烦大家帮忙看看问题出在哪?

测试代码(test_dockerized_tests.py)

from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options as chrome_options

class TestDockerized:
    def test_selenium_visit_a_site_and_validate(self):
        options = chrome_options()
        options.add_argument("--headless")
        # options.add_argument('--incognito')
        # options.add_argument('--disable-dev-shm-usage')
        driver = Chrome(options=options)
        driver.get('https://example.com')
        expected_title = 'Example Domain'
        actual_title = driver.title
        assert actual_title == expected_title

Dockerfile

FROM python:3.12
WORKDIR /practice-python

RUN apt-get update && apt-get install -y \
    wget \
    unzip \
    curl \
    gnupg \
    ca-certificates \
    fonts-liberation \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libcups2 \
    libdbus-1-3 \
    libgdk-pixbuf2.0-0 \
    libnspr4 \
    libnss3 \
    libx11-xcb1 \
    libxcomposite1 \
    libxdamage1 \
    libxrandr2 \
    xdg-utils \
    git \
    chromium \
    --no-install-recommends && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .
RUN pip install --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

COPY . .

ENV CHROME_BIN=/usr/bin/chromium
ENV CHROMEDRIVER_PATH=/usr/bin/chromedriver

CMD ["pytest", "-rA", "-v", "tests/test_dockerized_tests.py"]

requirements.txt

selenium==4.27.1
playwright==1.49.0
pytest==8.3.3
pytest-xdist==3.6.1
pytest-html==4.1.1
pytest-playwright==0.6.2
pytest-base-url==2.1.0
pytest-asyncio==0.25.0
python-dotenv==1.0.1
pytest-rerunfailures
allure-pytest==2.13.5
requests==2.32.3
urllib3==2.2.3
# psycopg2==2.9.10
psycopg2-binary==2.9.10
typing_extensions==4.12.2

完整错误栈信息

============================= test session starts ==============================
platform linux -- Python 3.13.3, pytest-8.3.3, pluggy-1.5.0 -- /usr/local/bin/python3.13
cachedir: .pytest_cache
metadata: {'Python': '3.13.3', 'Platform': 'Linux-6.11.0-1012-azure-x86_64-with-glibc2.36', 'Packages': {'pytest': '8.3.3', 'pluggy': '1.5.0'}, 'Plugins': {'rerunfailures': '15.0', 'xdist': '3.6.1', 'html': '4.1.1', 'allure-pytest': '2.13.5', 'base-url': '2.1.0', 'metadata': '3.1.1', 'playwright': '0.6.2', 'asyncio': '0.25.0'}, 'Base URL': ''}
rootdir: /practice-python
plugins: rerunfailures-15.0, xdist-3.6.1, html-4.1.1, allure-pytest-2.13.5, base-url-2.1.0, metadata-3.1.1, playwright-0.6.2, asyncio-0.25.0
asyncio: mode=Mode.STRICT, asyncio_default_fixture_loop_scope=None
collecting ... collected 1 items

tests/test_dockerized_tests.py::TestDockerized::test_selenium_visit_a_site_and_validate FAILED [100%]

=================================== FAILURES ===================================
____________ TestDockerized.test_selenium_visit_a_site_and_validate ____________
self = <tests.test_dockerized_tests.TestDockerized object at 0x7fb24ee7ead0>

def test_selenium_visit_a_site_and_validate(self):
    # driver = get_selenium_driver
    # driver.get('https://example.com')
    # expected_title = 'Example Domain'
    # actual_title = driver.title
    # print(f'Actual title = {actual_title}')
    # assert actual_title == expected_title
    options = chrome_options()
    options.add_argument("--headless")
    # options.add_argument('--incognito')
    # options.add_argument('--disable-dev-shm-usage')
>   driver = Chrome(options=options)
tests/test_dockerized_tests.py:20:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.13/site-packages/selenium/webdriver/chrome/webdriver.py:45: in __init__
    super().__init__(
/usr/local/lib/python3.13/site-packages/selenium/webdriver/chromium/webdriver.py:66: in __init__
    super().__init__(command_executor=executor, options=options)
/usr/local/lib/python3.13/site-packages/selenium/webdriver/remote/webdriver.py:241: in __init__
    self.start_session(capabilities)
/usr/local/lib/python3.13/site-packages/selenium/webdriver/remote/webdriver.py:329: in start_session
    response = self.execute(Command.NEW_SESSION, caps)["value"]
/usr/local/lib/python3.13/site-packages/selenium/webdriver/remote/webdriver.py:384: in execute
    self.error_handler.check_response(response)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x7fb24ed09940>
response = {'status': 500, 'value': '{"value":{"error":"session not created","message":"session not created: This version of ChromeDriver only supports Chrome version 128\nCurrent browser version is 127.0.6533.100 with binary path /usr/bin/chromium","stacktrace":"#0 0x55e0a7e0d353 <unknown>"}}'}

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 11:22:57