如何在Java的Selenium中通过ChromeOptions启用HTTP站点访问
Chrome 134+ 自动化测试无法访问HTTP站点的解决方案
技术栈
- OpenJDK Zulu21.40+17-CA (build 21.0.6+7-LTS)
- Google Chrome 134.0.6998.117(稳定版)
- Gradle、Junit5
- 系统:Fedora 41 / Windows 11(问题跨系统存在)
问题背景
你在为HTTP站点编写自动化测试时,Chrome的安全机制一直限制操作(比如无法下载图片),升级到134版本后,直接拦截站点访问并弹出安全警告,即使已尝试配置安全浏览相关参数,仍无法绕过限制。
已尝试的配置代码
public ChromeOptions getChromeOptions() { ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-safe-browsing-api"); options.addArguments("--safebrowsing-disable-download-protection"); String projectPath = System.getProperty("user.dir"); String downloadFilepath = projectPath + "\downloads"; HashMap<String, Object> chromePrefs = new HashMap<>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.default_directory", downloadFilepath); chromePrefs.put("safebrowsing.enabled", "false"); chromePrefs.put("download.prompt_for_download", "false"); options.setExperimentalOption("prefs", chromePrefs); return options; }
初始化代码:
public void initDriver() { if (driver == null) { ChromeOptions options = getChromeOptions(); driver = new ChromeDriver(options); properties(); } }
当前拦截提示
Secure connection is not supported for the abracadabra website Hackers can intercept and change the information that you exchange with this site.We recommend visiting this page later if you are connected to a public network. It is safest to use a reliable Wi-Fi network, such as a home or work network.You can also suggest that the website owner switch to HTTPS. Read more about this warning…
Go to the website (button)
Back (button)
有效解决方案(补充ChromeOptions配置)
关键修正与新增参数
- 修正偏好设置的布尔值类型:原代码中
safebrowsing.enabled设为字符串"false",Chrome无法识别,需改为布尔值false。 - 标记目标HTTP站点为安全来源:添加
--unsafely-treat-insecure-origin-as-secure=http://你的站点域名,直接将指定HTTP站点纳入安全信任范围,这是绕过134版本拦截的核心参数。 - 补充禁用安全限制的参数:添加以下参数强化绕过效果:
--allow-running-insecure-content:允许页面加载非HTTPS的资源--disable-web-security:禁用浏览器的同源策略与安全限制(仅测试环境使用)--disable-features=SafeBrowsingProtection,EnhancedSafeBrowsing:彻底禁用安全浏览相关保护功能
修改后的完整配置代码
public ChromeOptions getChromeOptions() { ChromeOptions options = new ChromeOptions(); // 核心:标记目标HTTP站点为安全来源,替换成你的实际站点URL options.addArguments("--unsafely-treat-insecure-origin-as-secure=http://abracadabra"); // 禁用安全浏览相关功能 options.addArguments("--disable-safe-browsing-api"); options.addArguments("--safebrowsing-disable-download-protection"); options.addArguments("--disable-features=SafeBrowsingProtection,EnhancedSafeBrowsing"); // 允许不安全内容加载与禁用web安全限制 options.addArguments("--allow-running-insecure-content"); options.addArguments("--disable-web-security"); String projectPath = System.getProperty("user.dir"); String downloadFilepath = projectPath + "\downloads"; HashMap<String, Object> chromePrefs = new HashMap<>(); chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.default_directory", downloadFilepath); // 修正为布尔值false chromePrefs.put("safebrowsing.enabled", false); chromePrefs.put("download.prompt_for_download", false); options.setExperimentalOption("prefs", chromePrefs); return options; }
注意事项
--unsafely-treat-insecure-origin-as-secure必须填写完整的站点URL(比如http://abracadabra或http://abracadabra:8080),多个站点用逗号分隔(如http://site1,http://site2)。--disable-web-security会关闭浏览器的核心安全机制,仅用于自动化测试环境,绝对不能用于日常浏览。
内容的提问来源于stack exchange,提问作者janikiplayaz
相关产品推荐
相关产品推荐

