我正在尝试使用 selenium webdriver 导航到 Google map ,同时在 Python 中模拟不同的位置。
这似乎不起作用:
from selenium import webdriver
driver = webdriver.Chrome()
params = {
"latitude": 42.1408845,
"longitude": -72.5033907,
"accuracy": 100
}
driver.execute_cdp_cmd("Emulation.setGeolocationOverride", params)
driver.get("https://maps.google.com")
当 Google map 打开时,我得到了我的当前位置,而不是 params
中设置的位置。
我做错了什么?使用 Selenium 4。
最佳答案
实际上你快到了。你只需要点击你的位置按钮。使用这个:
from selenium import webdriver
from selenium.webdriver.common.by import By #added
from selenium.webdriver.support.ui import WebDriverWait #added
from selenium.webdriver.support import expected_conditions as EC #added
driver = webdriver.Chrome()
params = {
"latitude": 42.1408845,
"longitude": -72.5033907,
"accuracy": 100
}
driver.execute_cdp_cmd("Emulation.setGeolocationOverride", params)
driver.get("https://maps.google.com")
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "widget-mylocation"))) #added
element.click(); #added
关于Python Selenium - 覆盖地理定位不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64133356/