I'm running Python/Chromedriver/GoogleChrome/Selenium headless on python, and I am having some trouble figuring out how to set a custom geolocation long / lat programmatically; the following code is not updating location on google.com or https://www.infobyip.com/browsergeolocation.php
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;"))
This does return the updated long / lat that I have set. BUT when using https://www.infobyip.com/browsergeolocation.php or http://google.com, it does not grab the new geolocation and does not work.
How would one set a custom geolocation with chromedriver config arguments, chrome dev tools programmatically, or by modifying files in my chrome's profile directory? The other answers for pythons selenium on stack overflow do not seem to work here.