【问题标题】:How can I allow location access using Selenium?如何允许使用 Selenium 访问位置?
【发布时间】:2015-02-08 04:37:42
【问题描述】:

我正在尝试使用 Java 中的 Selenium 来获取用户的地理坐标,但使用 IP 地址不够准确,所以我想使用这个网站 http://www.whataremycoordinates.com/,但它不起作用,我猜这是因为您必须允许使用位置,所以无论如何我可以允许在 Selenium 中使用位置,或者可能是其他方式来获取精确的地理坐标

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    通常,当网站想要获取此类数据时,浏览器会询问您是否要分享您的位置。问题在一个弹出窗口中,无法用selenium 控制。在这种情况下,您需要告诉浏览器,不要打开弹出窗口并允许共享您的位置,这样一开始就不会打开弹出窗口。

    对于Firefox,您需要:

    • 打开网站
    • 允许分享您的位置(您也可以查看about:permissions 以查看设置)
    • 保存当前的 Firefox 配置文件
    • FirefoxProfile 指向您之前保存的配置文件启动 Firefox

    有关详细信息,请参阅:

    【讨论】:

    • 有什么办法可以用 HtmlUnitDriver 做到吗?像具有驱动能力的东西
    【解决方案2】:

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

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

    lat-log-json:How to enable geolocation permissions for a website in firefox profile using selenium webdriver

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

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多