【问题标题】:How to enable geolocation permissions for a website in firefox profile using selenium webdriver如何使用 selenium webdriver 在 Firefox 配置文件中为网站启用地理定位权限
【发布时间】:2016-04-16 21:13:18
【问题描述】:

在使用 selenium webdriver 自动化站点时,我需要允许地理位置权限继续前进。我无法使用 Firefox 配置文件的功能来做到这一点。

Image of the geo-location pop up

类似的东西

   public static String fileName = "/Users/Arjit/Documents/geoLocation.json";
   WebDriver driver;
   FirefoxProfile profile = new FirefoxProfile();
   profile.setPreference("geo.enabled", true);
   profile.setPreference("geo.provider.use_corelocation", true);
   profile.setPreference("geo.wifi.uri",newFile(fileName).toURI().toString());
   driver = new FirefoxDriver(profile);
   driver.get("http://www.zoomcar.com");

geoLocation.json 有

{
    "status": "OK",
    "accuracy": 10.0,
    "location": {
        "lat": 12.9525060, 
        "lng": 77.6991510
     }
}

【问题讨论】:

  • 向我们展示您如何尝试使用capabilitiesFirefoxProfile

标签: firefox selenium webdriver profile


【解决方案1】:

我已经对您的脚本进行了以下更改,并且效果很好:

    profile.setPreference("geo.prompt.testing", true);
    profile.setPreference("geo.prompt.testing.allow", true);
    profile.setPreference("geo.wifi.uri", "file:///C:/Users/.../src/main/data/geoLocation.json"); // add absolute path here
    driver = new FirefoxDriver(profile);
    //driver.get("http://www.zoomcar.com");
    driver.get("http://html5demos.com/geo");

附: geoLocation.json 和你的一样,只是坐标不同。

{
    "status": "OK",
    "accuracy": 10.0,
    "location": {
        "lat": 18.976916,
        "lng": 73.736801
     }
}

【讨论】:

    【解决方案2】:

    对于 Selenium 3.0,这将起作用。如果您只想使用相同的配置文件。 您还可以通过键入获取要设置/更改的首选项列表 Firefox 地址栏中的“about:config”,然后按 Enter 键。

             FirefoxOptions op = new FirefoxOptions();
             op.SetPreference("geo.enabled", false);
             IWebDriver webDriver = new FirefoxDriver(op);
    

    在这里,我关闭了每次弹出的地理位置警报,我的测试正在运行。

    【讨论】:

      【解决方案3】:

      您可以在创建驱动程序时注入 Firefox 配置文件。

      我使用的是 Selenium 3。如果您使用的是 Selenium 2,您可以直接将配置文件传递给驱动程序。不需要 FirefoxOptions。

      lat-long-json: 我用过这段代码

      FirefoxOptions opt = getFirefoxOptions();
      WebDriver webDriver = new FirefoxDriver(opt);
      
      
      //method for fire fox profile//////////////////////////////////
           public static FirefoxProfile getFirefoxProfile() {
      
                  ProfilesIni profileIni = new ProfilesIni();
                  FirefoxProfile profile = profileIni.getProfile("webDriverProfile");
      
                  System.out.println("profile is null : " + (profile == null));
                  if (profile == null) {
                      profile = new FirefoxProfile();
                  }
      
                  profile.setPreference("browser.download.folderList", 2);
                  profile.setPreference("browser.download.dir", "download/path");
                  profile.setPreference(
                          "browser.helperApps.neverAsk.saveToDisk",
                          "application/pdf,application/octet-stream,"
                                  + "application/download,text/html,application/xhtml+xml");
                  profile.setPreference("pdfjs.disabled", true);
          //      profile.setPreference("dom.webnotifications.enabled", true);
                  profile.setPreference("geo.enabled", true);
                  profile.setPreference("geo.provider.use_corelocation", true);
                  profile.setPreference("geo.prompt.testing", true);
                  profile.setPreference("geo.prompt.testing.allow", true);
                  profile.setPreference("geo.wifi.uri", "path-to-loglatjson\\geo-location-ITPL.json");
                  // profile.setPreference("browser.helperApps.neverAsk.openFile",
                  // "application/pdf");
                  // profile.setPreference("browser.helperApps.alwaysAsk.force", false);
                  /*
                   * profile.setPreference("browser.download.manager.alertOnEXEOpen",
                   * false);
                   * profile.setPreference("browser.download.manager.focusWhenStarting",
                   * false); profile.setPreference("browser.download.manager.useWindow",
                   * false);
                   * profile.setPreference("browser.download.manager.showAlertOnComplete",
                   * false);
                   * profile.setPreference("browser.download.manager.closeWhenDone",
                   * false);
                   */
                  return profile;
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-08
        • 2011-11-28
        • 2015-07-29
        • 1970-01-01
        相关资源
        最近更新 更多