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

Chrome与ChromeDriver均为96版本时Selenium报Connection Reset问题求解

Selenium CDP版本不匹配及Connection Reset异常解决方案

问题根因

  • CDP版本警告是由于当前使用的Selenium版本未内置Chrome 96对应的CDP(Chrome DevTools Protocol)实现,只能降级匹配CDP 95版本,版本不兼容会导致DevTools WebSocket连接不稳定,容易被主动断开
  • 代码中使用driver.close()关闭窗口时,WebDriver后台持有的CDP WebSocket连接未被正常释放,会触发连接重置报错
  • Discord平台存在自动化工具检测机制,未做反爬配置的ChromeDriver访问时会被平台主动拦截断开连接

解决方案

  1. 升级Selenium依赖版本
    最低升级到Selenium 4.1.0及以上版本,该版本开始正式支持CDP v96,可完全匹配Chrome 96、ChromeDriver 96的运行环境,消除版本不匹配警告。

  2. 替换驱动关闭逻辑
    将代码末尾的driver.close()修改为driver.quit(),driver.quit()会主动销毁整个WebDriver实例,正常关闭所有后台连接,避免强制关闭窗口引发的连接重置异常。

  3. 添加Chrome反检测配置
    初始化ChromeDriver时添加反自动化识别的配置,绕过Discord的工具检测:

ChromeOptions options = new ChromeOptions();
// 移除自动化控制标识
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--disable-blink-features=AutomationControlled");
WebDriver driver = new ChromeDriver(options);
  1. 替换硬编码等待为显式等待
    替换代码中固定时长的Thread.sleep(),使用显式等待动态判断元素加载状态,减少无效等待时长,降低连接被主动断开的概率:
// 导入相关类:org.openqa.selenium.support.ui.WebDriverWait、org.openqa.selenium.support.ui.ExpectedConditions、java.time.Duration
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(100));
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div/div/div/div/div[2]/div[2]/div/aside/div/div/div[2]/div/div[2]/div[1]")));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 20:15:03