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
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:
about:permissions
to see the setting)FirefoxProfile
pointing to the profile you've saved beforeFor more information, see:
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With