6

I'm using selenium 2 - Webdriver , Chromedriver, Java. One of my tests needs to turn off the location service on Chrome .

I notice that by default, the location service is turned on Chromedriver.

I'm wondering if there is a way to disable location service on chromedriver?

many thanks in advance!

user1488025
  • 121
  • 5
  • 11

1 Answers1

5

Yes, it's possible. Use the ChromeOptions class, in the following way:

 ChromeOptions options = new ChromeOptions();

 JSONObject jsonObject = new JSONObject();
 jsonObject.put("profile.default_content_settings.geolocation", 2);

 options.setExperimentalOption("prefs", jsonObject);
 WebDriver driver = new ChromeDriver(options);

You can see it as an answer for an issue here

Johnny
  • 14,397
  • 15
  • 77
  • 118
  • How would you do it if the chrome options were in hashmap form? Map chromeOptions = new HashMap(); – elrobe Apr 27 '17 at 21:59
  • 1
    @elrobe I would try to convert the relevant entry from the Map to ChromeOptions. Please note that ChromeOptions.setExperimentalOption receive String and Object argument, that's exactly what you have in your map. Just copy the values from your map to as setExperimentalOption arguments. – Johnny Apr 30 '17 at 15:42
  • Just for a reference: JSONObject is created by this library [json-simple](https://code.google.com/archive/p/json-simple/). – jithinkmatthew Jan 06 '19 at 01:56
  • SetExperimentalOption is not available in chromeoptions anymore I think AddAdditionalCapability() will work – LordDraagon Feb 14 '19 at 05:52
  • or use the AddUserProfilePreference() method, which can be used instead using a jsonobject and can add the preferences directly to the method – LordDraagon Feb 14 '19 at 06:04