fake Geolocation in chrome automation

前端 未结 4 1831
别跟我提以往
别跟我提以往 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:32

    You can use execute_cdp but that is not found in webdriver/remote/remote_connection.py so you could do the following:

    chrome_geolocation_format = {
                "location": {
                    "latitude": 50.1109,
                    "longitude": 8.6821
                }
    
    driver.execute(Command.SET_LOCATION, chrome_geolocation_format)
    
    0 讨论(0)
  • 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;"))
    
    0 讨论(0)
  • 2021-01-16 23:37

    It's actually now progammable by using the Chrome Devtools Protocol (cdp):

    params = {
        "latitude": 50.1109,
        "longitude": 8.6821,
        "accuracy": 100
    }
    
    self.driver = webdriver.Chrome()
    self.driver.execute_cdp_cmd("Page.setGeolocationOverride", params)
    self.driver.get('https://www.google.com/maps')
    
    0 讨论(0)
  • 2021-01-16 23:45

    Using Python with Selenium 3.141 and ChromeDriver 83.0

    This is the working method I found:

    params = {
      "latitude": 34.0207305,
      "longitude": -118.6919153,
      "accuracy": 100
    }
    driver.execute_cdp_cmd("Emulation.clearGeolocationOverride", params)
    
    0 讨论(0)
提交回复
热议问题