如何使用Selenium WebDriver在FireFox配置文件中为网站启用地理定位权限 [英] How to enable geolocation permissions for a website in firefox profile using selenium webdriver

查看:339
本文介绍了如何使用Selenium WebDriver在FireFox配置文件中为网站启用地理定位权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Selenium Webdriver自动化站点时,我需要允许地理位置许可权继续向前发展.我无法使用firefox配置文件的功能来做到这一点.

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.

弹出的地理位置图片

类似这样的东西

   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具有

And the geoLocation.json has

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

推荐答案

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

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

我正在使用Selenium3.如果您正在使用Selenium 2,则可以将配置文件直接传递到驱动程序中.不需要FirefoxOptions.

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:我使用了这段代码

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

这篇关于如何使用Selenium WebDriver在FireFox配置文件中为网站启用地理定位权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆