Skip to content

Instantly share code, notes, and snippets.

@sqe
Last active December 10, 2018 03:50
Show Gist options
  • Save sqe/8e37b3f6f89d32496fb7 to your computer and use it in GitHub Desktop.
Save sqe/8e37b3f6f89d32496fb7 to your computer and use it in GitHub Desktop.
Faking geolocation in Chrome with javascript for @RubyTester
# 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()
@sqe
Copy link
Author

sqe commented Jan 23, 2016

Output:

37.773972:-122.431297
{
"results" : [
{
"address_components" : [
{
"long_name" : "716",
"short_name" : "716",
"types" : [ "street_number" ]
},
{
"long_name" : "Oak Street",
"short_name" : "Oak St",
"types" : [ "route" ]
},
{
"long_name" : "Western Addition",
"short_name" : "Western Addition",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "San Francisco",
"short_name" : "SF",
"types" : [ "locality", "political" ]
},
{
"long_name" : "San Francisco County",
"short_name" : "San Francisco County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94117",
"short_name" : "94117",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "716 Oak St, San Francisco, CA 94117, USA",
"geometry" : {
"location" : {
"lat" : 37.7740386,
"lng" : -122.4312261
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.77538758029149,
"lng" : -122.4298771197085
},
"southwest" : {
"lat" : 37.7726896197085,
"lng" : -122.4325750802915
}
}
},
"place_id" : "ChIJxTiUPaSAhYARvKf7nmE5Ids",
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Western Addition",
"short_name" : "Western Addition",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "San Francisco",
"short_name" : "SF",
"types" : [ "locality", "political" ]
},
{
"long_name" : "San Francisco County",
"short_name" : "San Francisco County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Western Addition, San Francisco, CA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.7903498,
"lng" : -122.4210075
},
"southwest" : {
"lat" : 37.7690383,
"lng" : -122.4476754
}
},
"location" : {
"lat" : 37.7822112,
"lng" : -122.4341781
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 37.7903498,
"lng" : -122.4210075
},
"southwest" : {
"lat" : 37.7690383,
"lng" : -122.4476754
}
}
},
"place_id" : "ChIJORmAxLuAhYARJN5IphkMohE",
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "San Francisco",
"short_name" : "SF",
"types" : [ "locality", "political" ]
},
{
"long_name" : "San Francisco County",
"short_name" : "San Francisco County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "San Francisco, CA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.9297707,
"lng" : -122.3279149
},
"southwest" : {
"lat" : 37.6931289,
"lng" : -123.166067
}
},
"location" : {
"lat" : 37.7749295,
"lng" : -122.4194155
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 37.812,
"lng" : -122.3482
},
"southwest" : {
"lat" : 37.70339999999999,
"lng" : -122.527
}
}
},
"place_id" : "ChIJIQBpAG2ahYAR_6128GcTUEo",
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "94117",
"short_name" : "94117",
"types" : [ "postal_code" ]
},
{
"long_name" : "San Francisco",
"short_name" : "SF",
"types" : [ "locality", "political" ]
},
{
"long_name" : "San Francisco County",
"short_name" : "San Francisco County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "San Francisco, CA 94117, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.8002219,
"lng" : -122.428426
},
"southwest" : {
"lat" : 37.75875,
"lng" : -122.469553
}
},
"location" : {
"lat" : 37.7717185,
"lng" : -122.4438929
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 37.7788508,
"lng" : -122.428426
},
"southwest" : {
"lat" : 37.75875,
"lng" : -122.462047
}
}
},
"place_id" : "ChIJ773z7quAhYARnGZ-5ZUIkcg",
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "San Francisco County",
"short_name" : "San Francisco County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "San Francisco County, CA, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 37.9297707,
"lng" : -122.3279149
},
"southwest" : {
"lat" : 37.6931289,
"lng" : -123.166067
}
},
"location" : {
"lat" : 37.7749073,
"lng" : -122.4193878
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 37.8332484,
"lng" : -122.3570189
},
"southwest" : {
"lat" : 37.7080859,
"lng" : -122.5177237
}
}
},
"place_id" : "ChIJIQBpAG2ahYARUksNqd0_1h8",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "California, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 42.0095169,
"lng" : -114.1313926
},
"southwest" : {
"lat" : 32.5342643,
"lng" : -124.415165
}
},
"location" : {
"lat" : 36.778261,
"lng" : -119.4179324
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 42.0095169,
"lng" : -114.1313926
},
"southwest" : {
"lat" : 32.5342643,
"lng" : -124.415165
}
}
},
"place_id" : "ChIJPV4oX_65j4ARVW8IJ6IJUYs",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "United States",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 71.3867745,
"lng" : -66.9502861
},
"southwest" : {
"lat" : 18.9106768,
"lng" : 172.4458955
}
},
"location" : {
"lat" : 37.09024,
"lng" : -95.712891
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 49.38,
"lng" : -66.94
},
"southwest" : {
"lat" : 25.82,
"lng" : -124.39
}
}
},
"place_id" : "ChIJCzYy5IS16lQRQrfeQ5K5Oxw",
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}

Copy link

ghost commented Jan 26, 2016

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:

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);}");

driver.get("http://html5demos.com/geo")

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

@sqe
Copy link
Author

sqe commented Jan 26, 2016

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 default_loc,

Then it re draws the map with the requested coordinates
desired_loc.

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)

Copy link

ghost commented Jan 26, 2016

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

@sqe
Copy link
Author

sqe commented Jan 26, 2016

nice, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment