【发布时间】:2017-07-20 03:35:42
【问题描述】:
【问题讨论】:
-
嗨,欢迎来到堆栈溢出。请参阅How to Ask 链接以获取有关如何提出问题并相应更新您的问题的更多详细信息。
标签: selenium selenium-chromedriver selendroid
【问题讨论】:
标签: selenium selenium-chromedriver selendroid
要禁用 chrome 询问位置,您需要使用 ChromeOptions 并从配置文件设置中禁用 geolocation
ChromeOptions options = new ChromeOptions();
JSONObject jsonObject = new JSONObject();
jsonObject.put("profile.default_content_settings.geolocation", 2);
options.setExperimentalOption("prefs", jsonObject);
WebDriver driver = new ChromeDriver(options);
请参阅this SO 帖子中已经解释了整个答案。
编辑:如果要保持启用该选项,您只需要更改此行
jsonObject.put("profile.default_content_settings.geolocation", 2);
到
jsonObject.put("profile.default_content_settings.geolocation", 1);
【讨论】:
add allow site :) 但您的代码正在禁用它。谢谢
rid of the popup on my testing 这让我相信他想忽略/禁用它。
Allow(总是)吗?谢谢
使用chrome devtools协议可以做到这一点。Browser.grantPermission允许在访问目标网站之前配置地理定位权限。您可以查看我的另一个答案以获取更多详细信息Setting sensors (location) in headless Chrome 。
【讨论】:
下面的代码 sn-p 可以让我禁用该弹出窗口。而不是profile.default_content_settings.geolocation,
我使用了profile.default_content_setting_values.notifications。
ChromeOptions options = new ChromeOptions();
JSONObject jsonObject = new JSONObject();
jsonObject.put("profile.default_content_setting_values.notifications", 1);
options.setExperimentalOption("prefs", jsonObject);
WebDriver driver = new ChromeDriver(options);
或
DesiredCapabilities caps = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("profile.default_content_setting_values.notifications", 1);
options.setExperimentalOption("prefs", prefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);
【讨论】: