【问题标题】:How to add allow site on location of chrome content setting with selenium如何使用硒在 chrome 内容设置的位置添加允许站点
【发布时间】:2017-07-20 03:35:42
【问题描述】:

我想使用 selenium 在移动 chrome 上添加网站“a.com”。 选项-[高级-内容设置-位置-访问前询问-允许站点]

因为我想在我的测试中摆脱弹出窗口

有人知道吗?

【问题讨论】:

  • 嗨,欢迎来到堆栈溢出。请参阅How to Ask 链接以获取有关如何提出问题并相应更新您的问题的更多详细信息。

标签: selenium selenium-chromedriver selendroid


【解决方案1】:

要禁用 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);

【讨论】:

  • OP 的问题是关于add allow site :) 但您的代码正在禁用它。谢谢
  • 感谢您的回答。我可以再问一个吗? “首选项”仅适用于桌面 Chrome。我必须在 Mobile Chrome 上做什么?
  • @DebanjanB OP 谈论 rid of the popup on my testing 这让我相信他想忽略/禁用它。
  • @LeBronPark 你可以使用移动仿真来实现这一点。 Here 是相同的链接。
  • @demouser123 你是对的(+1)你能帮我设置它Allow(总是)吗?谢谢
【解决方案2】:

使用chrome devtools协议可以做到这一点。Browser.grantPermission允许在访问目标网站之前配置地理定位权限。您可以查看我的另一个答案以获取更多详细信息Setting sensors (location) in headless Chrome

【讨论】:

    【解决方案3】:

    下面的代码 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);
    

    【讨论】:

      猜你喜欢
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 2021-04-19
      • 2016-05-18
      • 1970-01-01
      • 2018-01-06
      • 2013-08-21
      相关资源
      最近更新 更多