-
-
Save sqe/8e37b3f6f89d32496fb7 to your computer and use it in GitHub Desktop.
# Solution found @ http://stackoverflow.com/a/31803008: | |
from selenium import webdriver | |
import requests | |
driver = webdriver.Chrome() | |
fake_lat = "37.773972" | |
fake_long = "-122.431297" | |
# Sending latitude, longitude with JS script | |
driver.execute_script("window.navigator.geolocation.getCurrentPosition=function(success){"+ | |
"var position = {\"coords\" : {\"latitude\": \"%s\",\"longitude\": \"%s\"}};"+ | |
"success(position);}"); | |
# Printing latitude, longitude from the browser | |
print(driver.execute_script("var positionStr=\"\";"+ | |
"window.navigator.geolocation.getCurrentPosition(function(pos){positionStr=pos.coords.latitude+\":\"+pos.coords.longitude});"+ | |
"return positionStr;")) % (fake_lat, fake_long) | |
# Neat stuff from google maps api! | |
googlemap_url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s" % (fake_lat, fake_long) | |
get_data = requests.get(googlemap_url) | |
print get_data.content | |
driver.quit() |
Alright, this looks like an interesting challenge, what I think happening here is, that geolocation finds your real location by your IP, just changing the browser coordinates doesn't take any effect, as geolocation overrides the coordinates sent to map service and returns whatever it found.
The workaround I came up is to redraw the map with desired coordinates via javascript when the http://html5demos.com/geo is loaded.
Unfortunately I couldn't figure out how to allow location sharing in Chrome by default, so the script has a delay of 10, and you have to manually click the "Allow" button when it pops up, afterwards it finds your real location ,
Then it re draws the map with the requested coordinates
.
Not sure if helps, but it was fun looking at this problem.
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
fake_lat = "48.8567"
fake_long = "2.3508"
driver.get('http://html5demos.com/geo')
# Click Allow on chrome, I couldn't come up with a way to allow this by default, but I'm sure you know how
sleep(10)
# Requesting to draw a new map with desired coordinates, and puting the marker
javascript = '''
var latlng = new google.maps.LatLng(%s, %s);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeControl: false,
navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("mapcanvas"), myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"You are here!"
});
''' % (fake_lat, fake_long)
driver.execute_script(javascript)
The solution in Ruby in Remote Chrome is to use driver.set_location
after getting the driver - but first you have to pass a setting to chromedriver to accept the geolocation query and do not prompt the user: See this: https://gist.github.com/rubytester/26b81d9d77431f51380b#file-geoloc_chrome-rb-L15
That code starts a local driver (and set_location does not work there yet).
This works for remote driver:
https://gist.github.com/rubytester/26b81d9d77431f51380b
nice, thanks for sharing!
Then I must be doing this wrong in Ruby.
I assumed that if I setup a callback and I go to the url that calls the getCurrentPosition it would get the coords I set.
For example go to this page: html5demos.com/geo
In your code above I don't see
driver.get "url that asks for your geo"
So maybe I am a dumbass here and I don't understand how that's supposed to work
Thank you for the example. I will fire off python to test it.
OK, basically I was attempting something like this:
I magically thought the webpage would ask my driver instance browser for the geolocation and I would supply the given lat and lng.
but this does not happen. The webpage loads the map showing me my 'real' current Loc and not the fake one.
I must be a dumbass in all of this then