How to set a Mock Geolocation Long/Lat on Chromedriver Headless

1,846 views
Skip to first unread message

henry...@gmail.com

unread,
Oct 22, 2019, 10:08:49 PM10/22/19
to ChromeDriver Users
Hiya chromedriver-users,

I'm running chromedriver/google-chrome/seleniuim on python and am having some trouble figuring out how to set a custom geolocation long / lat programmaticly.

At the moment I'm starting chromedriver with these options:
chrome_options.add_experimental_option("prefs", { \
"profile.default_content_setting_values.media_stream_mic": 1, 
"profile.default_content_setting_values.media_stream_camera": 1,
"profile.default_content_setting_values.geolocation": 1, 
"profile.default_content_setting_values.notifications": 1,
"profile.default_content_settings.geolocation": 1,
"profile.default_content_settings.popups": 0
  })
I can call and set/get the geolocation long / lat with these commands:
driver.execute_script("""navigator.geolocation.getCurrentPosition = function(success, failure) { 
success({
coords: {latitude: -43.5333, longitude: 172.633}, 
timestamp: Date.now(),
}); 
}""");
time.sleep(5)
print(driver.execute_script("var positionStr=\"\";"+
"window.navigator.geolocation.getCurrentPosition(function(pos){positionStr=pos.coords.latitude+\":\"+pos.coords.longitude});"+
"return positionStr;"))
Now this does return the updated long / lat that I have set.
BUT when using https://www.infobyip.com/browsergeolocation.php or trying to get the location to detect on http://google.com it does not grab the new longitude / lat and does not work. I think this is because it's setting the geolocation after page load but I'm not 100%. I'm also trying to figure out if it is automaticly accepting the share geolocation notification but as I'm running headless I haven't been figure out how to know for certain.
Is their anyway I can set a custom geolocation with chromedriver config arguments, chrome dev tools programmatically or by modifying some files in my chromes profile? 
Any help would be much appreciated as you'd think their would be a way to set this up.

Cheers,
Henry

henry...@gmail.com

unread,
Oct 22, 2019, 11:19:43 PM10/22/19
to ChromeDriver Users
sorry to re-phrase the question slightly, but does anyone know how to set a custom mock sensor location using python/selenium/chromedriver and chromes remote devtools?

T Crichton

unread,
Oct 23, 2019, 1:33:38 PM10/23/19
to ChromeDriver Users
Unfortunately, there's not a really good answer for this today. But we can add full support in a future release.

First, you can enable geoLocation access for specific sites in the Preferences file in the test profile. The easiest way to set it is in the browser. 
Launch Chrome with the same command used by ChromeDriver (see the log file).
Navigate to the site needed, and accept the prompt to allow location access. After that, if you open settings, Site Settings, Location, the website should be listed under Allow. 

Then, to find the Profile directory: 
Open url chrome://version. Profile path is the profile folder. 
You will want to copy this complete folder to another location with a descriptive name. 
Preferences.txt contains the settings, but this file is not designed for human readability.

Then, when running ChromeDriver, you will have to add a command line argument for Chrome:
options.add_arguments("user-data-dir=<profiledir>")
where <profiledir> is a full path to the saved directory containing the modified preferences file.

Second, before w3c mode, ChromeDriver had a SetGeoLocation command. This was disabled for w3c mode since it's not in the new spec. We can reenable this for w3c mode. see https://crbug.com/chromedriver/3180 The Java bindings still have a driver.setLocation command, but I don't see it in the python bindings. Python does have driver.execute_cdp_cmd, which should be able to call the right command; I will confirm this once the command is reenabled.

I'm sorry I don't know any workarounds for you today.

T Crichton

unread,
Oct 25, 2019, 7:05:14 PM10/25/19
to ChromeDriver Users
Actually, using cdp_cmd does work now. This is how it would be used to set a mock location:

    params = {
      "latitude": 47.1111,
      "longitude": -122.1111,
      "accuracy": 100
    }
    response = driver.execute_cdp_cmd("Page.setGeolocationOverride", params)

Use negative for south latitude or west longitude. Accuracy is in meters; 100 is a default used in the code. I don't know what range is supported.
Reply all
Reply to author
Forward
0 new messages