Skip to content

Instantly share code, notes, and snippets.

@liquidgenius
Forked from sqe/faking_location.py
Created June 28, 2018 03:48
Show Gist options
  • Save liquidgenius/d07c1023d0200886677f757db9ab77f6 to your computer and use it in GitHub Desktop.
Save liquidgenius/d07c1023d0200886677f757db9ab77f6 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment