Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium - override geolocation not working

I'm trying to navigate to Google Maps using selenium webdriver while emulating a different location in Python.

This doesn't seem to work:

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

When Google Maps opens, I get my current location rather than the one set in params.

What am I doing wrong? Using Selenium 4.

like image 890
vandernath Avatar asked May 25 '26 20:05

vandernath


1 Answers

Actually you are almost there. you just need to click the your location button. using this:

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
like image 101
Yash Avatar answered May 28 '26 08:05

Yash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!