Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable geolocation support in chromedriver?

I need to test a JS geolocation functionality with Selenium and I am using chromedriver to run the test on the latest Chrome.

The problem is now that Chrome prompts me to enable Geolocation during the test and that I don't know how to click that little bar on runtime, so I am desperately looking for a way to start the chromedriver and chrome with some option or trigger to enable this by default. All I could find here was however how I can disable geolocation altogether.

How can I solve this issue?

like image 289
Thomas Keller Avatar asked Dec 07 '11 07:12

Thomas Keller


People also ask

How do I enable geolocation?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

What is default location ChromeDriver?

Default location on Windows is: C:\Program Files\(select the folder you want to put your file)\chromedriver.exe. In your Selenium code, paste the driver path correctly, for example: System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Java\\chromedriver.exe");


2 Answers

I took your method but it can not working. I did not find "BaseUrls.Root.AbsoluteUri" in Chrome's Preferences config. And use a script for test

chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_argument("proxy-server=http://127.0.0.1:1087")
    prefs = {
        'profile.default_content_setting_values':
            {
                'notifications': 1,
                'geolocation': 1
            },
        'devtools.preferences': {
            'emulation.geolocationOverride': "\"[email protected]:\"",
        },
        'profile.content_settings.exceptions.geolocation':{
            'BaseUrls.Root.AbsoluteUri': {
                'last_modified': '13160237885099795',
                'setting': '1'
            }
        },
        'profile.geolocation.default_content_setting': 1

    }

    chromeOptions.add_experimental_option('prefs', prefs)
    chromedriver_path = os.path.join(BASE_PATH, 'utils/driver/chromedriver')
    log_path = os.path.join(BASE_PATH, 'utils/driver/test.log')
    self.driver = webdriver.Chrome(executable_path=chromedriver_path, chrome_options=chromeOptions, service_log_path=log_path)
like image 57
ESing Avatar answered Oct 02 '22 17:10

ESing


As for your initial question:

You should start Firefox manually once - and select the profile you use for Selenium.

Type about:permissions in the address line; find the name of your host - and select share location : "allow".

That's all. Now your Selenium test cases will not see that dreaded browser dialog which is not in the DOM.

like image 24
tbsalling Avatar answered Oct 02 '22 16:10

tbsalling