【问题标题】:fake Geolocation in chrome automationchrome自动化中的假地理位置
【发布时间】:2015-07-31 21:41:55
【问题描述】:
我需要使用 python 脚本在 chrome 中自动进行地理定位。我必须伪造纬度和经度。我关注了stackoverflow中的一些链接,但他们给出了错误。
chromeDriver.executeScript("window.navigator.geolocation.getCurrentPosition=function(success){var position = {"coords" : {"latitude": "555","longitude": "999"}};success(position); }");
有没有其他方法可以改变位置?
【问题讨论】:
标签:
python
selenium-webdriver
gps
selenium-chromedriver
【解决方案1】:
在 Selenium 3.141 和 ChromeDriver 83.0 中使用 Python
这是我找到的工作方法:
params = {
"latitude": 34.0207305,
"longitude": -118.6919153,
"accuracy": 100
}
driver.execute_cdp_cmd("Emulation.clearGeolocationOverride", params)
【解决方案2】:
现在实际上可以使用 Chrome Devtools 协议 (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')
【解决方案3】:
你必须在字符串中转义双引号
请尝试以下代码:
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;"))
【解决方案4】:
您可以使用 execute_cdp 但在 webdriver/remote/remote_connection.py 中找不到它,因此您可以执行以下操作:
chrome_geolocation_format = {
"location": {
"latitude": 50.1109,
"longitude": 8.6821
}
driver.execute(Command.SET_LOCATION, chrome_geolocation_format)