fake Geolocation in chrome automation

前端 未结 4 1865
别跟我提以往
别跟我提以往 2021-01-16 23:07

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.

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-16 23:33

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

提交回复
热议问题