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

Selenium+ChromeForTesting禁用「始终使用安全连接」方法求助

如何在Selenium + ChromeForTesting中禁用Chrome「始终使用安全连接」选项

目标网站采用HTTP协议部署在服务器上,Chrome浏览器「隐私和安全」->「安全」路径下的「始终使用安全连接」选项会导致访问时持续弹出「不支持安全连接」提示。当前使用ChromeForTesting版本为132.0.6834.83,已在DriverFactory中配置多项ChromeOptions参数(代码如下),但问题仍未解决:

private static ChromeOptions getChromeOptions() {
    ChromeOptions chromeOptions = new ChromeOptions();
    HashMap<String, Object> chromePrefs = new HashMap<>();
    chromePrefs.put("download.default_directory", localDownloadDir);
    chromePrefs.put("download.directory_upgrade", true);
    chromePrefs.put("download.prompt_for_download", false);
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("safebrowsing.enabled", true);
    chromeOptions.setExperimentalOption("prefs", chromePrefs);
    chromeOptions.setPageLoadStrategy(PageLoadStrategy.NORMAL);   
    chromeOptions.addArguments("--lang=en");
    chromeOptions.addArguments("--enable-popup-blocking");
    chromeOptions.addArguments("--disable-infobars"); 
    chromeOptions.addArguments("--disable-notifications"); 
    chromeOptions.addArguments("--disable-extensions");
    chromeOptions.addArguments("--no-sandbox");
    chromeOptions.addArguments("--allow-running-insecure-content");
    chromeOptions.addArguments("--disable-web-security");
    chromeOptions.addArguments("--allow-insecure-localhost");
    chromeOptions.addArguments("--unsafely-treat-insecure-origin-as-secure");
 
    File downloadDir = new File(localDownloadDir);
    if (!downloadDir.exists()) {
        downloadDir.mkdirs();
    }
    if (isHeadless) {
        chromeOptions.addArguments("--headless=new");
    }
    
    if ("false".equalsIgnoreCase(Config.getBuildParam("chromeVerboseLogging"))) {
        System.setProperty("webdriver.chrome.silentOutput", "true");
    }

    if ("t".equalsIgnoreCase(Config.getBuildParam("insecurecerts"))) {
        chromeOptions.setAcceptInsecureCerts(true);
        chromeOptions.addArguments("--ignore-certificate-errors");
        chromeOptions.addArguments("--ignore-ssl-errors=yes");
        chromeOptions.addArguments("--ignore-certificate-errors-spki-list");
    }

    return chromeOptions;
}

解决方法

「始终使用安全连接」选项对应的Chrome内部偏好设置键是security.always_use_secure_connection,只需在你的chromePrefs中添加该键并设置为false即可禁用该选项:

chromePrefs.put("security.always_use_secure_connection", false);

修改后的完整代码片段

在原代码的chromePrefs初始化部分加入上述配置:

private static ChromeOptions getChromeOptions() {
    ChromeOptions chromeOptions = new ChromeOptions();
    HashMap<String, Object> chromePrefs = new HashMap<>();
    chromePrefs.put("download.default_directory", localDownloadDir);
    chromePrefs.put("download.directory_upgrade", true);
    chromePrefs.put("download.prompt_for_download", false);
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("safebrowsing.enabled", true);
    // 添加这一行禁用「始终使用安全连接」
    chromePrefs.put("security.always_use_secure_connection", false);
    chromeOptions.setExperimentalOption("prefs", chromePrefs);
    // 其余代码保持不变...

补充说明

  • 该偏好设置直接对应Chrome设置界面中的「始终使用安全连接」开关,设置为false后会强制关闭该功能,避免HTTP站点的安全提示弹窗。
  • 132版本的ChromeForTesting完全兼容该偏好键,无需额外配置其他参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 21:43:11