问题
I am creating some end-to-end tests for a web app using Selenium.
I am working in Python and using the Firefox driver
driver = webdriver.Firefox()
The problem is that my web app using HTML5 geolocation, and it seems that everytime I run my tests, I have to click the 'Allow Location' popup in Firefox, making my tests less than automated.
Is there a way to force the Selenium Firefox driver to always allow geolocation without prompting?
回答1:
I believe the default is to launch Firefox with a new, anonymous profile. You can launch selenium with -Dwebdriver.firefox.profile=whatever, where "whatever" is the name of a profile when you launch firefox -P.
To make sure there's no weirdness with persistent logins and other cookies:
- Launch Firefox with "firefox -P"
- Pick the profile you'll launch the tests with
- Edit -> Preferences -> Privacy, select Use custom settings for history
- Tell Firefox to keep cookies until "I close Firefox"
回答2:
You can force browser to return some predefined location without permission requests.
Just execute the following JavaScript code:
"navigator.geolocation.getCurrentPosition = function(success) { success({coords: {latitude: 50.455755, longitude: 30.511565}}); }"
Tested in Firefox and Chrome.
回答3:
Because I reached the same problem almost 3 years after this question was asked, and None of above answers were satisfying for me. I like to show solution I use.
So the answer I found on this blog.
And use it on my python code this way:
@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
is my path to JSON
file:
{
"status": "OK",
"accuracy": 10.0,
"location": {
"lat": 50.850780,
"lng": 4.358138,
"latitude": 50.850780,
"longitude": 4.358138,
"accuracy": 10.0
}
}
回答4:
Just stumbled upon this problem today and I knew there should exist a simple and quick way of dealing with this. Here's how I solved it:
- Create a new Firefox profile that will be used by Selenium. You can do it by typing
about:profiles
and then clicking on 'Create new profile'. A step by step instruction can be found in Mozilla's docs. - Head to the site that needs the permissions and wait for it to ask you to grant them. Grant them and check the "Remember this decision" box.
You'll 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 an "example_profile" and then copied the "Root directory" path:You can then pass the path as a parameter while instantiating a Firefox browser like this:
root_directory_path = "/.../Firefox/Profiles/loeiok2p.example_profile" driver = webdriver.Firefox(firefox_profile=root_directory_path)
Selenium should use the profile with granted permissions and the popup should not reappear.
回答5:
Here is more refined answer from one of the answers above. This adds some timeout before doing geolocation success callback, as usually JavaScript is written in such a manner that geolocation coordinates are not expected to be available until there has been one cycle back to the event loop.
This allows also tracing the spoofing through 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", " "))
回答6:
Would it be enough to just allow it one time manually and then allow python do to the job?
Because you can easily allow by setting this in the website properties in Firefox:

来源:https://stackoverflow.com/questions/16292634/always-allow-geolocation-in-firefox-using-selenium