首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何允许使用Selenium访问位置?

如何允许使用Selenium访问位置?
EN

Stack Overflow用户
提问于 2015-02-08 04:37:42
回答 2查看 6.8K关注 0票数 5

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-08 04:56:00

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

对于Firefox,您需要:

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

有关更多信息,请参见:

票数 8
EN

Stack Overflow用户

发布于 2018-11-28 08:19:48

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

我使用的selenium 3,如果您使用selenium 2,而不需要firefoxOptions,您可以直接将概要文件传递给驱动程序。

lat-log-json:如何使用selenium为firefox配置文件中的网站启用地理位置权限

代码语言:javascript
运行
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28390611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档