3

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.

vandernath
  • 3,665
  • 3
  • 15
  • 24

2 Answers2

2

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
Yash
  • 1,271
  • 1
  • 7
  • 9
  • Thank you, I guess that does fix the problem. However I'd rather have my location set before I even reach the website (to reduce the amount of interactions with the website itself). Is there any way to do this? Also, I realize this works for Google Maps, but what if the site does not have a "location" button and displays different content depending on location? – vandernath Sep 30 '20 at 11:14
  • I would consider this a workaround and not an answer – Imran Apr 13 '22 at 22:02
0

https://ifconfig.co/ defines the actual coordinates, not the ones passed by cdp.