Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何允许使用Selenium进行位置访问?_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java 如何允许使用Selenium进行位置访问?

Java 如何允许使用Selenium进行位置访问?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我试图在Java中使用Selenium来获取用户的地理坐标,但是使用IP地址不够准确,所以我想使用这个网站,但它不起作用,我猜这是因为你必须允许使用位置,所以我可以在Selenium中使用位置,或者通过其他方式获取准确的地理坐标通常,当站点想要获取此类数据时,浏览器会询问您是否要共享您的位置。问题在弹出窗口中,无法用selenium控制。在这种情况下,您需要告诉浏览器,不要打开弹出窗口,同时允许共享您的位置,这样弹出窗口一开始就不会被打开 对于Firefox,您需要: 打开站点 允许共享您的位

我试图在Java中使用Selenium来获取用户的地理坐标,但是使用IP地址不够准确,所以我想使用这个网站,但它不起作用,我猜这是因为你必须允许使用位置,所以我可以在Selenium中使用位置,或者通过其他方式获取准确的地理坐标通常,当站点想要获取此类数据时,浏览器会询问您是否要共享您的位置。问题在弹出窗口中,无法用
selenium
控制。在这种情况下,您需要告诉浏览器,不要打开弹出窗口,同时允许共享您的位置,这样弹出窗口一开始就不会被打开

对于
Firefox
,您需要:

  • 打开站点
  • 允许共享您的位置(您也可以检查
    about:permissions
    查看设置)
  • 保存当前的firefox配置文件
  • 启动firefox时,
    FirefoxProfile
    指向您以前保存的配置文件
有关详细信息,请参阅:


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

我使用的是selenium 3,如果您使用的是selenium 2,则无需firefoxOptions,您可以直接将配置文件传递到驱动程序中

lat日志json:


有什么方法可以用HtmlUnitDriver实现吗?比如说一些有驱动能力的东西
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;
        }