7

When we write something like this:

FirefoxProfile ffprofile = new FirefoxProfile(new File("D:\\Selenium"));

Does it mean we are creating a new Profile? Because I won't be able to find any new profile in the Firefox Profile section.

So now my question is, how do I create a new profile for a Firefox browser?

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Niyati
  • 493
  • 3
  • 5
  • 20

5 Answers5

7

The method call you stated simply creates a java profile object from the given directory of profile information which is then passed to Firefox via the WebDriver instance.

In order to get Firefox to persist your driver and make it available from profile manager, you need to edit the file profiles.ini, on my (Windows 7) machine this was in:

%APPDATA%\Roaming\Mozilla\Firefox

The Profiles directory within this folder contains the stores of existing Firefox profiles, which are quite handy to copy when you want to use an existing profile as the template for a new one.

Your mileage may vary depending on your OS, but I'm sure you can find it with a quick search. Using your example, you'd then add the following to this file (where N in the header is the next unused profile number):

[ProfileN]
Name=selenium
IsRelative=0
Path=D:\Selenium

This will cause Firefox Profile Manager to load the profile and will allow you to then launch Firefox manually with this profile to configure or test it, which is what I presume you want to do.

Once you have created a named profile in this way, you can assign it to your driver in Selenium like this:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("selenium");
WebDriver driver = FirefoxDriver(profile);

Where "selenium" is the same as the Name property in the profiles.ini file.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Dave Birch
  • 469
  • 4
  • 10
  • 1
    How ProfilesIni knows the path? we are just giving name... as the path of profiles.ini is different from system to system. – Lokesh Sanapalli Feb 10 '16 at 14:46
  • The selenium firefox driver implementation for your system knows how to find the profiles.ini for your specific system install of Firefox. You then define within profiles.ini the path to store your profile data in, this you can configure to look wherever you want on your system. – Dave Birch Feb 10 '16 at 15:27
  • getProfile function create a copy of the given profile. Thus, running in this profile doesn't store the values as normal profile does (for example, remember password won't work, etc.) – Loc Phan Dec 11 '17 at 03:25
  • Correct path for profile.ini is `%APPDATA%\Mozilla\Firefox`. APPDATA already contains `\Roaming` – Daniel Alder Dec 11 '17 at 11:01
3

You cannot create a profile for firefox using Selenium. What you can do is create a firefox profile for your webdriver from the available profiles in firefox. Firefox profile word sounds bit ambiguous here.

To create a firefox profile in browser, refer Mozilla support page for details.

Manu
  • 2,251
  • 19
  • 30
2

Following code will create firefox profile (based on provided file) and create a new FF webdriver instance with this profile loaded:

FirefoxProfile profile = new FirefoxProfile(new File("D:\\Selenium Profile"));
WebDriver driver = new FirefoxDriver(profile);

Maybe take a look on the official support page for FF profile manager or here: Custom Firefox profile for Selenium to get some idea on FF profiles.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Michal
  • 3,218
  • 2
  • 25
  • 44
  • 1
    I have use above code but when i search new profile using "firefox.exe -ProfileManager -no-remote" command than it doesn't show any of the new profile – Niyati Oct 14 '13 at 09:09
2

Here is how I do by selenium 3 with geckodriver:

  • Use firefox command line interface to create profile

    firefox.exe -CreateProfile "profile_name profile_dir"
    

    (In java, execute this runtime by Runtime.getRuntime().exec function)

  • Set -profile argument in firefox options

    FirefoxOptions options = new FirefoxOptions();
    options.addArguments("-profile", <profile_dir>);
    driver = new FirefoxDriver(options);
    
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Loc Phan
  • 4,304
  • 4
  • 29
  • 35
  • Nice to know about this. Looks like a good path to be able to programmatically create and set a new Profile for Firefox. This may be the solution to workaround the Firefox driver unable to handle the proxy authentication dialog. It does not implement the HasCredentials interface that would prevent the proxy auth dialog to appear when we use the driver.get(url). When this dialog appears selenium throws an exception. I tried to use sendKeys to fill the username and password but it throws another exception saying the dialog authentication is not supported to send keys. – cavila Jan 03 '22 at 15:09
-2

Create profile in Firefox browser.

Here is the code for using newly created firefox profile.

ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("firefox profile name");
WebDriver driver = new FirefoxDriver(myprofile);
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Ankan
  • 9
  • 2