如何使用Selenium WebDriver在Firefox配置文件中启用网站的地理位置权限

3
在使用Selenium WebDriver自动化网站时,我需要允许地理位置权限以继续进行。使用Firefox配置文件的能力无法实现此操作。

地理位置弹出窗口的图像

类似于这样

   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
     }
}

1
展示一下您如何尝试使用capabilitiesFirefoxProfile - Andersson
3个回答

2
我已经尝试了您的脚本并进行了以下更改,它可以正常工作:
    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");

P.S. geoLocation.json与您的文件类似,但具有不同的坐标。

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

Results page


1
对于那些几年后来到这里的人,这个方法也适用于我。 - Derrick Wright
1
对于以后几年来到这里的任何人,这个方法对我也有效。 - undefined

1
对于Selenium 3.0,这将起作用。如果您只想使用相同的配置文件。 此外,您可以通过在Firefox地址栏中键入“about:config”并按Enter键来获取要设置/更改的首选项列表。
         FirefoxOptions op = new FirefoxOptions();
         op.SetPreference("geo.enabled", false);
         IWebDriver webDriver = new FirefoxDriver(op);

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


1
您可以在创建驱动程序时注入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;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接