Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a specific Firefox profile in Selenium WebDriver in C#

I am trying to use a profile I already have set up for firefox with selenium 2 but there is no documentation for C#. The code I have attempted is as follows:

FirefoxProfileManager profileManager = new FirefoxProfileManager();
FirefoxProfile profile = profileManager.GetProfile(profileName);
driver = new FirefoxDriver(profile);

Code that I have seen that is comparible in Java uses ProfilesIni instead of FirefoxProfileManager, but that is not available in C#. When setting up the driver in this way the selenium profile used has all the default settings instead of the settings specified in the profile I am trying to point to.

I am not sure that I am using the correct methods to retrieve the profile, but if anyone has used Selenium 2 with C#, any information would be helpful.

like image 280
Jeff Macdonald Avatar asked Sep 30 '11 19:09

Jeff Macdonald


People also ask

What is the use of Firefox profile in Selenium WebDriver?

Firefox profiles include custom preferences that you would like to simulate an environment for your test script. For example, you might want to create a profile that sets preferences to handle the download popup programmatically during your test run.

How do I use Firefox options in Selenium?

FirefoxOptions options = new FirefoxOptions(); driver = new RemoteWebDriver(new URL("http://10.x.x.x:4444/wd/hub"), options); When you start your Selenium Nodes, it displays a log information on using new FirefoxOptions preferred to 'DesiredCapabilities. firefox() along with all other browser options.

How do I create an object for my Firefox driver?

We can create Object of a class FirefoxDriver by taking reference of an interface (WebDriver). In this case, we can call implemented methods of WebDriver interface. As per the above statement, we are creating an instance of the WebDriver interface and casting it to FirefoxDriver Class.

How do I transfer my Firefox profile to another account?

For example: '''Overview''' These are the steps described in more detail below: # Create a new Firefox profile folder # Open the new profile folder and exit Firefox # Remove everything from that new profile folder # Copy in everything from the old profile folder '''Create a New Profile''' Inside Firefox, type or paste ...


2 Answers

We use such method to load default firefox profile (you can create custom profile and load it):

private IWebDriver driver;  
string pathToCurrentUserProfiles = Environment.ExpandEnvironmentVariables("%APPDATA%") + @"\Mozilla\Firefox\Profiles"; // Path to profile
string[] pathsToProfiles = Directory.GetDirectories(pathToCurrentUserProfiles, "*.default", SearchOption.TopDirectoryOnly);
if (pathsToProfiles.Length != 0)
{
     FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]);
     profile.SetPreference("browser.tabs.loadInBackground", false); // set preferences you need
     driver = new FirefoxDriver(new FirefoxBinary(), profile, serverTimeout);
}
else
{
     driver = new FirefoxDriver();
}
like image 168
thezar Avatar answered Sep 22 '22 04:09

thezar


We had the same problem that the profile wouldn't load. The problem is in FirefoxProfile (line 137). It only looks for user.js and the profile from Firefox is actually prefs.js

137>> File prefsInModel = new File(model, "user.js");

Hack solution: rename prefs.js --> user.js

like image 45
toddb Avatar answered Sep 22 '22 04:09

toddb