如何解决关闭“...想知道您的位置”对话框
我正在使用 Selenium + Chromedriver 自动测试网站。其中一项功能使用定位服务。我已为我使用的 Chrome 浏览器启用定位服务。
但是当我运行测试时,会显示下图所示的对话框。
以下代码找到警报 - 但 alert.dismiss() 似乎不起作用(即对话框仍然存在)
try {
Alert alert = driver.switchTo().alert();
alert.dismiss();
} catch (NoAlertPresentException ex) {
// System.out.println("No alert for this test case.");
}
测试代码如何单击“允许”按钮并关闭对话框?我的目标是点击允许。
--山姆--
解决方法
Map<String,Object> prefs = new HashMap<String,Object>();
Map<String,Object> profile = new HashMap<String,Object>();
prefs.put("googlegeolocationaccess.enabled",true);
prefs.put("profile.default_content_setting_values.geolocation",2); // 1:allow 2:block
prefs.put("profile.default_content_setting_values.notifications",1);
prefs.put("profile.managed_default_content_settings",1);
options.setExperimentalOption("prefs",prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
webDriver = new ChromeDriver(capabilities);
您也可以尝试设置 prefs.put("googlegeolocationaccess.enabled",false);
这将根据您在运行驱动程序之前设置的标志默认阻止或接受所有地理位置访问请求
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。