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
相关产品推荐
相关产品推荐

