Selenium Java如何处理Edge与Safari浏览器地理位置权限弹窗
Selenium Java 多浏览器地理位置授权弹窗自动处理方案
在基于Selenium Java开展多浏览器自动化测试时,可通过浏览器启动预配置实现地理位置授权弹窗的自动放行/拦截,无需人工介入操作,各浏览器可落地的配置方案如下:
Firefox 浏览器配置
通过FirefoxProfile设置地理位置相关偏好参数即可实现弹窗自动处理,对应实现代码:
FirefoxProfile geoDisabled = new FirefoxProfile(); geoDisabled.setPreference("geo.enabled", false); geoDisabled.setPreference("geo.provider.use_corelocation", false); geoDisabled.setPreference("geo.prompt.testing", true); geoDisabled.setPreference("geo.prompt.testing.allow", true); caps.setCapability(FirefoxDriver.PROFILE,geoDisabled);
Chrome 浏览器配置
通过ChromeOptions注入实验性偏好配置实现权限控制,权限标识规则:0=默认、1=允许授权、2=阻止授权,对应实现代码:
ChromeOptions chromeOptions = new ChromeOptions(); // SET CHROME OPTIONS // 0 - Default, 1 - Allow, 2 - Block int locationFlag = location.equals(true) ? 1:2; Map<String, Object> prefs = new HashMap<>(); Map<String, Object> profile = new HashMap<> (); Map<String, Object> contentSettings = new HashMap<> (); contentSettings.put("geolocation", locationFlag); profile.put("managed_default_content_settings", contentSettings); prefs.put("profile", profile); chromeOptions.setExperimentalOption("prefs", prefs);
MS Edge 浏览器配置
经实测,Edge可采用和Chrome逻辑一致的偏好注入方案实现弹窗处理,同时支持搭配InPrivate无痕模式参数启动,对应实现代码:
EdgeOptions edgeOptions = new EdgeOptions(); if(isPrivate) { edgeOptions.addArguments("inprivate"); } int locationFlag = location.equals(true) ? 1:2; Map<String, Object> prefs = new HashMap<>(); Map<String, Object> profile = new HashMap<> (); Map<String, Object> contentSettings = new HashMap<> (); contentSettings.put("geolocation", locationFlag); profile.put("managed_default_content_settings", contentSettings); prefs.put("profile", profile); edgeOptions.setExperimentalOption("prefs", prefs); caps.merge(edgeOptions);
Safari 浏览器适配现状
截至目前,暂未验证到可稳定生效的Safari浏览器地理位置授权弹窗自动处理方案,该场景的无人工介入实现仍待探索。
内容的提问来源于stack exchange,提问作者user1826602
相关产品推荐
相关产品推荐

