Always allow geolocation in Firefox using Selenium

I am creating some end-to-end tests for a web application using Selenium .

I work in Python and use the Firefox driver

driver = webdriver.Firefox()

The problem is that my web application is using HTML5 geolocation, and it seems that every time I run my tests, I need to click the "Allow Location" pop-up in Firefox, making my tests less automatic.

Is there a way to make the Selenium Firefox driver always allow geolocation without a hint?

+4
source share
6 answers

I believe that Firefox starts with a new anonymous profile by default. You can start selenium with -Dwebdriver.firefox.profile = no matter where "anything" is the profile name when running firefox -P.

To make sure there is no weirdness with persistent entries and other cookies:

  • Launch Firefox with "firefox -P"
  • Select the profile to which you will start the tests with
  • Edit → Settings → Privacy, select Use custom settings for history
  • Tell Firefox to save cookies until I close Firefox.
0
source

You can force the browser to return a specific predefined location without requesting a request.

Just execute the following JavaScript code:

 "navigator.geolocation.getCurrentPosition = function(success) { success({coords: {latitude: 50.455755, longitude: 30.511565}}); }" 

Tested in Firefox and Chrome.

+7
source

Because I achieved the same problem almost 3 years after this question was asked, and none of the above answers satisfied me. I like to show the solution I'm using.

So, the answer I found on this blog .

And use it on my Python code as follows:

 @classmethod def setUpClass(cls): cls.binary = FirefoxBinary(FF_BINARY_PATH) cls.profile = FirefoxProfile() cls.profile.set_preference("geo.prompt.testing", True) cls.profile.set_preference("geo.prompt.testing.allow", True) cls.profile.set_preference('geo.wifi.uri', GEOLOCATION_PATH) cls.driver = Firefox(firefox_binary=cls.binary, firefox_profile=cls.profile) 

On GEOLOCATION_PATH - my path to the JSON file:

 { "status": "OK", "accuracy": 10.0, "location": { "lat": 50.850780, "lng": 4.358138, "latitude": 50.850780, "longitude": 4.358138, "accuracy": 10.0 } } 
+2
source

Today I came across this problem, and I knew that there should be an easy and quick way to deal with it. Here's how I solved it:

  1. Create a new Firefox profile that will be used by Selenium. You can do this by typing about:profiles and then clicking Create New Profile. Step-by-step instructions can be found in the Mozilla docs .
  2. Go to a site that needs permissions and wait for it to ask you to provide them. Provide them and check the box next to "Remember this solution."
  3. You need to know where your profile is stored so that Selenium can use it. You can find the location in about:profiles . Here I created "example_profile" and then copied the path "Root directory": about: profiles

  4. You can then pass the path as a parameter when creating the Firefox browser as follows:

     root_directory_path = "/.../Firefox/Profiles/loeiok2p.example_profile" driver = webdriver.Firefox(firefox_profile=root_directory_path) 

Selenium should use the profile with the permissions granted, and the popup should not appear again.

+1
source

Here is a more subtle answer from one of the answers above. This adds some timeout before making a callback with a successful geolocation, because, as a rule, JavaScript is written in such a way that the geolocation coordinates will not be available until the cycle loop is returned in one cycle.

It also allows you to track spoofing through the web console.

 SPOOF_LOCATION_JS = """ (function() { console.log('Preparing geo spoofing'); navigator.geolocation.getCurrentPosition = function(success) { console.log("getCurrentPosition() called"); setTimeout(function() { console.log("Sending out fake coordinates"); success({coords: {latitude: 50.455755, longitude: 30.511565}}); }, 500); }; console.log("Finished geospoofing")})(); """ browser.evaluate_script(SPOOF_LOCATION_JS.replace("\n", " ")) 
0
source

Is it enough to just allow it once manually, and then let python do the job?

Because you can easily resolve it by setting this in the website properties in Firefox: enter image description here

-2
source

All Articles