fake Geolocation in chrome automation

﹥>﹥吖頭↗ 提交于 2020-01-11 12:54:08

问题


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:


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


来源:https://stackoverflow.com/questions/31755633/fake-geolocation-in-chrome-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!