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

在抽象类中创建Chrome WebDriver触发版本不匹配异常问题

问题描述

在普通类中直接初始化ChromeDriver可正常运行,但通过抽象基类ChromeDriverBase继承实现子类ChromeChild时,触发SessionNotCreatedException,提示Chrome版本不匹配。但实际本地安装的Chrome(109.0.5414.119)与ChromeDriver(109.0.5414.74)版本完全匹配。

普通类正常运行代码

class example:
    def __init__(self) -> None:
        remote_debug_port=8956
        options = webdriver.ChromeOptions()
        options.add_argument(f'--remote-debugging-port={remote_debug_port}')
        options.add_argument("--start-maximized")
        options.add_argument('--no-sandbox')
        options.add_argument('--headless')
        options.add_argument('--disable-infobars')
        options.add_argument('--disable-dev-shm-usage')
        driver = webdriver.Chrome("/usr/bin/chromedriver", options=options)

a = example()

抽象类及子类代码

from abc import ABC, abstractmethod

from selenium import webdriver


class ChromeDriverBase(ABC):
    def __init__(self, url_source:str,
                webdriver_path:str="/usr/bin/chromedriver",
                wait_time:int=10,
                remote_port:int=2345) -> None:
        self.url_source = url_source
        self.wait_time = wait_time
        self.remote_port = remote_port
        self.create_options()
        self.driver = webdriver.Chrome(webdriver_path, options=self.options)
    
    def create_options(self):
        self.options = webdriver.ChromeOptions()
        self.options.add_argument(f'--remote-debugging-port={self.remote_port}')
        self.options.add_argument("--start-maximized")
        self.options.add_argument('--no-sandbox')
        self.options.add_argument('--headless')
        self.options.add_argument('--disable-infobars')
        self.options.add_argument('--disable-dev-shm-usage')
        
    
    @abstractmethod
    def function1(self):
        pass


class ChromeChild(ChromeDriverBase):
    def __init__(self, url_source: str, wait_time: int = 10, remote_port: int = 2345) -> None:
        super().__init__(url_source=url_source, wait_time=wait_time, remote_port=remote_port)


    def function1(self):
        # just for illustration
        return self.url_source


a = ChromeChild(url_source='www.google.com')

错误信息

selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 109
Current browser version is 103.0.5060.114 with binary path /usr/bin/google-chrome

本地版本检查结果

Google Chrome 109.0.5414.119 
ChromeDriver 109.0.5414.74
原因分析
  1. Remote Debugging端口被旧版本Chrome占用:抽象类使用的remote_port=2345可能已被系统残留的旧版本Chrome实例占用,导致ChromeDriver连接到旧版本浏览器,触发版本不匹配错误。而普通类使用的8956端口未被占用,因此能正常连接到新版本Chrome。
  2. Chrome二进制路径未明确指定:系统中可能同时存在多个Chrome版本,未在ChromeOptions中指定binary_location时,Selenium可能调用了旧版本Chrome(即错误提示中的103版本),而非你安装的109版本。普通类运行时的环境上下文可能默认调用了正确的二进制文件。
解决步骤

方法一:更换端口并清理旧Chrome进程

  • 修改抽象类中的remote_port参数,比如换成普通类使用的8956,或其他未被占用的端口;
  • 运行代码前关闭所有Chrome进程,避免旧实例占用端口:
    pkill -f chrome
    

方法二:明确指定Chrome二进制路径

在create_options方法中添加binary_location配置,指向109版本Chrome的实际路径(可通过which google-chrome-stable命令确认路径):

def create_options(self):
    self.options = webdriver.ChromeOptions()
    # 指定正确的Chrome二进制文件路径
    self.options.binary_location = "/usr/bin/google-chrome-stable"
    self.options.add_argument(f'--remote-debugging-port={self.remote_port}')
    self.options.add_argument("--start-maximized")
    self.options.add_argument('--no-sandbox')
    self.options.add_argument('--headless')
    self.options.add_argument('--disable-infobars')
    self.options.add_argument('--disable-dev-shm-usage')

方法三:更新Selenium版本

若上述方法无效,可能是Selenium版本兼容性问题,尝试更新到稳定版本:

pip install --upgrade selenium

内容的提问来源于stack exchange,提问作者LearnToGrow

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 12:51:17