Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I allow location access using Selenium?

I am trying to use Selenium in Java to get the user's geographical coordinates, but using IP address isn't accurate enough, so i wanted to use this website http://www.whataremycoordinates.com/ , but it isn't working, and I am guessing it is because you have to allow location use, so is there anyway I can allow location use in Selenium, or maybe some other way to get exact geographical coordinates

like image 804
Steve Avatar asked Feb 08 '15 04:02

Steve


2 Answers

Usually, when a site wants to get this kind of data, a browser asks you if you want to share your location. The question is inside a popup which cannot be controlled with selenium. In this case, you need to tell the browser, not to open the popup and allow to share your location at the same time so that the popup would not be opened in the first place.

For Firefox, you need to:

  • open the site
  • allow to share your location (you can also check about:permissions to see the setting)
  • save the current firefox profile
  • start firefox with FirefoxProfile pointing to the profile you've saved before

For more information, see:

  • Custom Firefox Profile for Selenium
  • How to open specified profile Firefox with Selenium 2 Webdriver?
  • How to use custom Firefox Profile with Selenium? (Java) (And pass HTML Authorization Window)
  • Change GeoLocation using Selenium WebDriver in Firefox
  • Webdriver: Click "Share Location" button in Firefox
like image 50
alecxe Avatar answered Oct 18 '22 18:10

alecxe


you can inject firefox profile at the time of creating driver

selenium 3 I am using if you are using selenium 2 no need for firefoxOptions you can directly pass the profile into driver.

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;
        }
like image 26
sarat chandra Avatar answered Oct 18 '22 18:10

sarat chandra