fake Geolocation in chrome automation

2019-03-05 23:49发布

I need to automate geolocation in chrome using python script. I have to fake the latitude and longitude. I followed some links in stackoverflow but they gave errors. chromeDriver.executeScript("window.navigator.geolocation.getCurrentPosition=function(success){var position = {"coords" : {"latitude": "555","longitude": "999"}};success(position);}");

is there any other way i can change the location?

1条回答
Deceive 欺骗
2楼-- · 2019-03-06 00:26

You have to escape double quotes inside a string please try with below code:

driver.execute_script("window.navigator.geolocation.getCurrentPosition=function(success){"+
                                    "var position = {\"coords\" : {\"latitude\": \"555\",\"longitude\": \"999\"}};"+
                                    "success(position);}");

print(driver.execute_script("var positionStr=\"\";"+
                                "window.navigator.geolocation.getCurrentPosition(function(pos){positionStr=pos.coords.latitude+\":\"+pos.coords.longitude});"+
                                "return positionStr;"))
查看更多
登录 后发表回答