使用Selenium Java处理边缘和野生动物园的位置弹出窗口

发布于 2025-02-10 21:27:31 字数 2175 浏览 3 评论 0 原文

我正在尝试处理多个浏览器的Selenium Java的位置弹出窗口。我无法找到MS Edge和Safari的解决方案。我试图实现与以下浏览器相同的浏览器:

                FirefoxProfile geoDisabled = new FirefoxProfile();
                geoDisabled.setPreference("geo.enabled", false);
                geoDisabled.setPreference("geo.provider.use_corelocation", false);
                geoDisabled.setPreference("geo.prompt.testing", true);
                geoDisabled.setPreference("geo.prompt.testing.allow", true);
                caps.setCapability(FirefoxDriver.PROFILE,geoDisabled);

或在Chrome中为:

               ChromeOptions chromeOptions = new ChromeOptions();

                // SET CHROME OPTIONS
                // 0 - Default, 1 - Allow, 2 - Block
                int locationFlag = location.equals(true) ? 1:2;

                Map< String, Object > prefs = new HashMap<>();
                Map < String, Object > profile = new HashMap <> ();
                Map < String, Object > contentSettings = new HashMap <> ();
                contentSettings.put("geolocation", locationFlag);
                profile.put("managed_default_content_settings", contentSettings);
                prefs.put("profile", profile);
                chromeOptions.setExperimentalOption("prefs", prefs);

更新: 以下解决方案为MSEDGE工作:

EdgeOptions edgeOptions = new EdgeOptions();
                if(isPrivate) {
                    edgeOptions.addArguments("inprivate");
                }
                int locationFlag = location.equals(true) ? 1:2;
                Map< String, Object > prefs = new HashMap<>();
                Map < String, Object > profile = new HashMap <> ();
                Map < String, Object > contentSettings = new HashMap <> ();
                contentSettings.put("geolocation", locationFlag);
                profile.put("managed_default_content_settings", contentSettings);
                prefs.put("profile", profile);
                edgeOptions.setExperimentalOption("prefs", prefs);
                caps.merge(edgeOptions);

在Safari上仍然没有进展。

I am trying to handle location popups in selenium java for multiple browsers. I am not able to find out the solution for MS Edge and Safari. I am trying to achieve the same as below for these browser:

                FirefoxProfile geoDisabled = new FirefoxProfile();
                geoDisabled.setPreference("geo.enabled", false);
                geoDisabled.setPreference("geo.provider.use_corelocation", false);
                geoDisabled.setPreference("geo.prompt.testing", true);
                geoDisabled.setPreference("geo.prompt.testing.allow", true);
                caps.setCapability(FirefoxDriver.PROFILE,geoDisabled);

or in Chrome as :

               ChromeOptions chromeOptions = new ChromeOptions();

                // SET CHROME OPTIONS
                // 0 - Default, 1 - Allow, 2 - Block
                int locationFlag = location.equals(true) ? 1:2;

                Map< String, Object > prefs = new HashMap<>();
                Map < String, Object > profile = new HashMap <> ();
                Map < String, Object > contentSettings = new HashMap <> ();
                contentSettings.put("geolocation", locationFlag);
                profile.put("managed_default_content_settings", contentSettings);
                prefs.put("profile", profile);
                chromeOptions.setExperimentalOption("prefs", prefs);

Update:
Following solution worked for MSEdge:

EdgeOptions edgeOptions = new EdgeOptions();
                if(isPrivate) {
                    edgeOptions.addArguments("inprivate");
                }
                int locationFlag = location.equals(true) ? 1:2;
                Map< String, Object > prefs = new HashMap<>();
                Map < String, Object > profile = new HashMap <> ();
                Map < String, Object > contentSettings = new HashMap <> ();
                contentSettings.put("geolocation", locationFlag);
                profile.put("managed_default_content_settings", contentSettings);
                prefs.put("profile", profile);
                edgeOptions.setExperimentalOption("prefs", prefs);
                caps.merge(edgeOptions);

There is still no progress on Safari.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猛虎独行 2025-02-17 21:27:31

对于边缘:

EdgeOptions edgeOptions = new EdgeOptions();
                if(isPrivate) {
                    edgeOptions.addArguments("inprivate");
                }
                int locationFlag = location.equals(true) ? 1:2;
                Map< String, Object > prefs = new HashMap<>();
                Map < String, Object > profile = new HashMap <> ();
                Map < String, Object > contentSettings = new HashMap <> ();
                contentSettings.put("geolocation", locationFlag);
                profile.put("managed_default_content_settings", contentSettings);
                prefs.put("profile", profile);
                edgeOptions.setExperimentalOption("prefs", prefs);
                caps.merge(edgeOptions);

到目前为止,Safari不支持操纵位置prefs。

For edge:

EdgeOptions edgeOptions = new EdgeOptions();
                if(isPrivate) {
                    edgeOptions.addArguments("inprivate");
                }
                int locationFlag = location.equals(true) ? 1:2;
                Map< String, Object > prefs = new HashMap<>();
                Map < String, Object > profile = new HashMap <> ();
                Map < String, Object > contentSettings = new HashMap <> ();
                contentSettings.put("geolocation", locationFlag);
                profile.put("managed_default_content_settings", contentSettings);
                prefs.put("profile", profile);
                edgeOptions.setExperimentalOption("prefs", prefs);
                caps.merge(edgeOptions);

Safari doesn't support as of now to manipulate location prefs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文