Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable geolocation in selenium-chromedriver with python

Want to disable automatic geoloaction in Chrome using Chromedriver when I visit a https website.

Tryed:

from selenium.webdriver.chrome.options import Options
chromeOptions = webdriver.ChromeOptions()
prefs = {"profile.default_content_settings.geolocation" : "2"}
chromeOptions.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

And:

from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=chrome_options)

Both doesn't work because on every new chrome window created with chromedriver the geolocation is enabled.

like image 260
urinella Avatar asked Dec 19 '22 12:12

urinella


2 Answers

your pref key is incorrect, below code worked for me

options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.geolocation" :2}
options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(chrome_options=options)
like image 160
saurabh baid Avatar answered Jan 31 '23 01:01

saurabh baid


Below code is working for me on July 2018

    ChromeOptions options = new ChromeOptions();
    Map<String, Object> prefs = new HashMap<String, Object>();
    prefs.put("profile.managed_default_content_settings.geolocation", 2);
    options.setExperimentalOption("prefs", prefs);
    ChromeDriver = new ChromeDriver(options);

Saludos.

like image 41
Rick Richard Avatar answered Jan 31 '23 03:01

Rick Richard