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

IEDriverServer 3.12.0报InvalidArgumentException:ensureCleanSession是已知问题吗?

解决IEDriverServer 3.12.0中ensureCleanSession引发的异常

嘿,我来帮你搞定这个IEDriver的异常问题!这个报错并不是你代码写法的问题,而是Selenium 3.12.0版本里IEDriver的一个兼容性小bug——它默认会自动添加ensureCleanSession这个旧协议的参数,但这个参数在W3C协议验证下会被判定为“未知能力”,所以触发了InvalidArgumentException。

下面给你两种可行的修复方案,选哪种都能解决问题:

方案一:用InternetExplorerOptions处理会话清理

推荐用这种方式,更贴合新版Selenium的API风格:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;

public class StartInternetExplorer {
    public static void main(String[] args) {
        String executable = "C:\\IEDriverServer_Win32_3.12.0\\IEDriverServer.exe";
        System.setProperty("webdriver.ie.driver", executable);
        
        // 创建IE选项,用官方推荐的方法处理会话清理
        InternetExplorerOptions options = new InternetExplorerOptions();
        options.destructivelyEnsureCleanSession(); 
        // 如果你想直接关闭这个参数,也可以用下面这句代替上面的方法
        // options.setCapability("ie.ensureCleanSession", false);
        
        WebDriver driver = new InternetExplorerDriver(options);
        driver.get("https://www.google.com");
        driver.quit();
    }
}

方案二:用DesiredCapabilities显式禁用该参数

如果你习惯用旧版的DesiredCapabilities写法,也可以这样调整:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class StartInternetExplorer {
    public static void main(String[] args) {
        String executable = "C:\\IEDriverServer_Win32_3.12.0\\IEDriverServer.exe";
        System.setProperty("webdriver.ie.driver", executable);
        
        DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
        // 显式设置该参数为false,避免驱动自动添加引发冲突
        caps.setCapability("ie.ensureCleanSession", false);
        
        WebDriver driver = new InternetExplorerDriver(caps);
        driver.get("https://www.google.com");
        driver.quit();
    }
}

额外说明

这个问题是Selenium 3.x向W3C协议过渡时的兼容性问题,后续版本(比如Selenium 3.141.59及以上)已经修复了这个默认行为。如果你的项目允许升级,更新到更高版本的Selenium或IEDriverServer,也能彻底避免这个问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:41:35