How to enable geolocation permissions for a website in firefox profile using selenium webdriver

不羁岁月 提交于 2019-12-11 10:08:48

问题


While automating a site using selenium webdriver, i am required to allow the geo-location permissions to move ahead. I am not able to do it using capabilites of firefox profile.

Image of the geo-location pop up

Something like this

   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");

And the geoLocation.json has

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

回答1:


I've tried your script with the following changes and it worked:

    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 is just like yours but with different coordinates.

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




回答2:


You can inject Firefox profile at the time of creating the driver.

I am using Selenium 3. If you are using Selenium 2 you can directly pass the profile into driver. There's no need for FirefoxOptions.

lat-long-json: I used this code

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



回答3:


For Selenium 3.0 this will work. If you just want to use the same profile. Also you can get a list of preferences to set/change by Typing "about:config" in your firefox's address bar and hitting Enter key.

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

Here, I am turning off the geo-location alert that pops up every time, my test is run.



来源:https://stackoverflow.com/questions/34740547/how-to-enable-geolocation-permissions-for-a-website-in-firefox-profile-using-sel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!