10

I am trying to trick the chromedriver to make it believe that it is running in a different city. Under normal circumstances, this can easily be done manually as shown in a quick diagram

Here

Then, when a google search is done, the new coordinates are used, and the results that would normally originate from that location are displayed. You can confirm that this worked when you look at the bottom of a Google search page as seen

Here.

However, Selenium can only control what the browser displays, not the browser in itself. I cannot tell Selenium to automatically click the buttons needed to change the coordinates. I tried the solutions posted here but that is not meant for Python, and even after I tried to adapt the script, nothing seemed to happen.

Is there a browser.execute_script() argument that could work, or is this the wrong way to change the geolocation?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ari
  • 111
  • 1
  • 6
  • I'd suggest adding the images here directly so the question will never go out of date. With that this question deserves an upvote. – AER Dec 14 '18 at 02:26
  • Could be to do with user permissions if you're interacting with another app? – AER Dec 14 '18 at 02:30
  • @Ari Have you been able to fix the geolocation change? I'm looking for the same thing and I'm finding it impossible – Apalabrados Oct 25 '19 at 11:08

4 Answers4

2

You can do this by importing Selenium DevTools package. Please refer below for complete java code sample:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;

public void geoLocationTest(){
  ChromeDriver driver = new ChromeDriver();
  Map coordinates = new HashMap()
  {{
      put("latitude", 50.2334);
      put("longitude", 0.2334);
      put("accuracy", 1);
  }};    
  driver.executeCdpCommand("Emulation.setGeolocationOverride", coordinates);
  driver.get("<your site url>");
}  

Reference : Selenium Documentation

Vijendran Selvarajah
  • 1,330
  • 1
  • 11
  • 31
1

Try this code below :

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;"))
An Nguyen
  • 288
  • 1
  • 15
  • While I did see the print statement print out the coordinates which I asked for, there was no effect on Google Search results. Normally, going to the next page of results will update the location when using the sensor method. When I tried that with this execute_script, there was no change, it still shows my actual location. Something to note, [here](https://i.imgur.com/Mp3mG9E.png) you can see that Google has gotten my location form my internet address, whereas, using the sensor method, it gets location "reported from this computer". Is there a way to prevent chrome from seeing my IP? – Ari Dec 14 '18 at 23:27
  • I suggest you should use fake IP app. – An Nguyen Dec 17 '18 at 01:20
0

In search for a solution to the same problem I also came across the already postet scripts. While I am yet to find a solution, I assume that the scripts don't work because they do not change the sensors permanently. The sensors are only changed for that one specific call of window.navigator.geolocation.getCurrentPosition.

The website (in this case google) will later call the same function but with the regular (unchanged) geolocation. Happy to hear solutions to permanently change the sensors to then also affect future geolocation requests.

pcsso
  • 121
  • 2
  • 9
0

This Can be Done Using Selenium 4.

HashMap<String ,Object> coordinate = new HashMap<String ,Object>();
        coordinate.put("latitude", 39.913818);
        coordinate.put("longitude", 116.363625);
        coordinate.put("accuracy", 1);
    ((ChromeDriver)driver).executeCdpCommand("Emulation.setGeolocationOverride",coordinate);
        driver.navigate().to("URL");