简体   繁体   English

如何在 Python/Chromedriver/Selenium 中设置模拟地理位置 Long/Lat

[英]How to set mock Geolocation Long/Lat in Python/Chromedriver/Selenium

I'm running Python/Chromedriver/GoogleChrome/Selenium headless on python, and I am having some trouble figuring out how to set a custom geolocation long / lat programmatically;我在 python 上无头运行 Python/Chromedriver/GoogleChrome/Selenium,但在弄清楚如何以编程方式设置自定义地理位置 long/lat 时遇到了一些麻烦; the following code is not updating location on google.com or https://www.infobyip.com/browsergeolocation.php以下代码未更新 google.com 或https://www.infobyip.com/browsergeolocation.php上的位置

At the moment I'm starting chromedriver with these options:目前我正在使用以下选项启动 chromedriver:

chrome_options.add_experimental_option("prefs", { 
    "profile.default_content_setting_values.media_stream_mic": 1, 
    "profile.default_content_setting_values.media_stream_camera": 1,
    "profile.default_content_setting_values.geolocation": 1, 
    "profile.default_content_setting_values.notifications": 1,
    "profile.default_content_settings.geolocation": 1,
    "profile.default_content_settings.popups": 0
  })

I can call and set/get the geolocation long / lat with these commands:我可以使用以下命令调用并设置/获取地理位置 long / lat:

    driver.execute_script("""navigator.geolocation.getCurrentPosition = function(success, failure) { 
      success({ 
        coords: {latitude: -43.5333, longitude: 172.633}, 
        timestamp: Date.now(), 
      }); 
    }"""); 
    time.sleep(5) 
    print(driver.execute_script("var positionStr=\"\";"+ 
                                "window.navigator.geolocation.getCurrentPosition(function(pos){positionStr=pos.coords.latitude+\":\"+pos.coords.longitude});"+
                                "return positionStr;"))

This does return the updated long / lat that I have set.这确实返回了我设置的更新的 long / lat。 BUT when using https://www.infobyip.com/browsergeolocation.php or http://google.com , it does not grab the new geolocation and does not work. BUT when using https://www.infobyip.com/browsergeolocation.php or http://google.com , it does not grab the new geolocation and does not work.

How would one set a custom geolocation with chromedriver config arguments, chrome dev tools programmatically, or by modifying files in my chrome's profile directory?如何使用 chromedriver config arguments、chrome dev 工具以编程方式或通过修改我的 chrome 配置文件目录中的文件来设置自定义地理位置? The other answers for pythons selenium on stack overflow do not seem to work here.关于堆栈溢出的 python selenium 的其他答案似乎在这里不起作用。

It's actually now progammable by using the Chrome Devtools Protocol (cdp):它现在实际上可以通过使用 Chrome Devtools 协议 (cdp) 进行编程:

params = {
    "latitude": 50.1109,
    "longitude": 8.6821,
    "accuracy": 100
}

driver = webdriver.Chrome()
driver.execute_cdp_cmd("Page.setGeolocationOverride", params)
driver.get('https://www.google.com/maps')

Linux Distribution Linux分布

Firefox Webdriver Selenium Version: 3.14.1 Firefox Webdriver Selenium 版本:3.14.1

GeckoDriver: https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz GeckoDriver: https://github.com/mozilla/geckodriver/releases/download/v0.26.0/geckodriver-v0.26.0-linux64.tar.gz

  • Use the default Profile and modify it's settings使用默认配置文件并修改其设置
  • Use profile with webdriver iteratively迭代地使用带有 webdriver 的配置文件

Default profile, ex: /home/-user-/.mozilla/firefox/wg8ugmge.default默认配置文件,例如:/home/-user-/.mozilla/firefox/wg8ugmge.default

Executable Path, ex: /home/-user-/Downloads/geckodriver可执行路径,例如:/home/-user-/Downloads/geckodriver

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

profile = webdriver.FirefoxProfile('<path_to_default_profile>') 
profile.set_preference("geo.prompt.testing", True) 
profile.set_preference("geo.prompt.testing.allow", True)
profile.set_preference("geo.wifi.scan", True)
executable_path='<executable-path-geckodriver>'
profile.set_preference("geo.wifi.uri", 'data:application/json,{"location":{"lat":22.893,"lng":72.4095},"accuracy": 100.0}')

driver = webdriver.Firefox(executable_path = executable_path , firefox_profile=profile)
driver.get('https://maps.mapmyindia.com')

Hope it solves your problem !希望它能解决你的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM