Last active
December 10, 2018 03:50
-
-
Save sqe/8e37b3f6f89d32496fb7 to your computer and use it in GitHub Desktop.
Faking geolocation in Chrome with javascript for @RubyTester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
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-L15That code starts a local driver (and set_location does not work there yet).
This works for remote driver:
https://gist.github.com/rubytester/26b81d9d77431f51380b