I'm using python 2.7.1 and selenium 2.20.0.
I have a separate Firefox profile called "selenium" which I want to use when running the webdriver.
I managed to do it by specifying the complete path to the profile as shown below, but that's not very portable code.
profile = FirefoxProfile("/Users/username/Library/Application Support/Firefox/Profiles/jtokyple.selenium")
self.driver = webdriver.Firefox(profile)
Is there any better way to specify the profile?
-
I do not have that much idea. Thank you for sharing this question.William Hruska– William Hruska2022年08月30日 09:03:16 +00:00Commented Aug 30, 2022 at 9:03
6 Answers 6
If you want it to be portable, you need to have the firefox profile included in your build and copied to the working directory. You should be able to reference it from there easily. This will work even if you copy all of your tests to a different machine and execute them there.
I've been using the following as a constant:
FF_PROFILE_PATH = os.path.join(os.environ['APPDATA'],
'Mozilla', 'Firefox', 'Profiles')
I believe it's consistent across OSes per Mozilla support documentation. To locate the correct profile, you can iterate through the list created by os.listdir
to find the profile, as such:
try:
profiles = os.listdir(FF_PROFILE_PATH)
except WindowsError:
print "Could not find profiles directory."
sys.exit(1)
try:
loc = (folder for folder in profiles
if folder.endswith(profile)).next()
except StopIteration:
print "Firefox profile not found."
sys.exit(1)
return os.path.join(FF_PROFILE_PATH, loc)
Using WebDriver you can add any addons and change any configurations you want from the programming code. It's not required to create separate firefox profile
-
Great, so suppose I want to have a setting where files get downloaded automatically without a popup to a particular directory. I can set it manually in Firefox and it works for my user, but not when Selenium is driving it. How can I change this in code?Leonid– Leonid2012年11月20日 00:12:33 +00:00Commented Nov 20, 2012 at 0:12
Base on Liam Kirsh answer I prepared full working code for Windows (in my case Windows 10 and Python 3.4):
import os
import sys
from selenium import webdriver
def get_profile_path(profile):
FF_PROFILE_PATH = os.path.join(os.environ['APPDATA'],'Mozilla', 'Firefox', 'Profiles')
try:
profiles = os.listdir(FF_PROFILE_PATH)
except WindowsError:
print("Could not find profiles directory.")
sys.exit(1)
try:
for folder in profiles:
print(folder)
if folder.endswith(profile):
loc = folder
except StopIteration:
print("Firefox profile not found.")
sys.exit(1)
return os.path.join(FF_PROFILE_PATH, loc)
profile = get_profile_path("default")
print(profile)
driver = webdriver.Firefox(firefox_profile=profile)
I think, I rushed and just added code. Tested Ok below code with Selenium 2.4, python 2.7 Linux OS: mint17 Debian base.
# for fresh FF profile
#profile = webdriver.FirefoxProfile()
profile = webdriver.FirefoxProfile("./firefox_profiles/selenium")
profile = webdriver.FirefoxProfile(profile_path)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("http://whatismyip.com")
I feel, it's good idea to have absolute path. In fact, put it in conf file so you can use it for various deployment instances and OSes. Relative path have to start from location where you have selenium .py file. It should work in same way on Windows. NOT tested on windows.
-
1Your answer doesn't actually solve the problem stated in the question. You skipped the part of how to specify a relative profile path, which is what was asked. Additionally your answer would be better if you would include some explanation with your code.Edu– Edu2015年09月08日 07:22:31 +00:00Commented Sep 8, 2015 at 7:22
-
1
profile = webdriver.FirefoxProfile(profile_path)
Theprofile_path
variable is not defined in any part of that code. Do not add that line if there's no variable, this makes code not understable and not runnable at all. It should be commented like the other line...m3nda– m3nda2018年01月16日 07:54:05 +00:00Commented Jan 16, 2018 at 7:54
for my, this is correct:
from selenium import webdriver
profile = webdriver.FirefoxProfile('/home/user-name-pc/.mozilla/firefox/1kj45idd.default')
driver = webdriver.Firefox(executable_path=your_path_selenium, firefox_profile=profile)
driver.get('https://hello.com')