开发者

Using a specific Firefox profile in Selenium WebDriver in C#

开发者 https://www.devze.com 2023-04-09 10:27 出处:网络
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:

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.


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();
}


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


The following worked for me. I had to specifically set the "webdriver.firefox.profile" preference in order to get it to work.

        var allProfiles = new FirefoxProfileManager();

        if (!allProfiles.ExistingProfiles.Contains("SeleniumUser"))
        {
            throw new Exception("SeleniumUser firefox profile does not exist, please create it first.");
        }
        var profile = allProfiles.GetProfile("SeleniumUser");

        profile.SetPreference("webdriver.firefox.profile", "SeleniumUser");

        WebDriver = new FirefoxDriver(profile);


I have the same issue, it is not a duplicate.

I am using the following which works

private IWebDriver Driver;

[Setup]
public void SetupTest()
{
string path = @"C:\Users\username\AppData\Local\Mozilla\Firefox\Profiles\myi5go1k.default";
FirefoxProfile ffprofile = new FirefoxProfile(path);
Driver = new FirefoxDriver(ffprofile);
}


After using the aforementioned answers I had no result, so I tried the following alternate way:

First, create the desired profile by typing about:profiles on Firefox address bar.

Second, the C# code. Note that the profile name we created on first step is passed as an argument.

public IWebDriver driver { get; set; }
    
public Selenium(String nombrePefil)
{
    if (this.driver == null)
    {
        FirefoxOptions options = new FirefoxOptions();                     

        options.AddArgument("--profile " + nombrePefil);

        this.driver = new FirefoxDriver(options);                        
                    
    }
}


I also encountered the same issue, and after searching and trying many different combinations I was able to get Selenium to load a specific profile when using the RemoteWebDriver.

Grid configuration

I launch the HUB using a batch file containing the following

"C:\Program Files (x86)\Java\jre6\bin\java.exe" -jar C:\Downloads\Selenium\selenium-server-standalone-2.20.0.jar -role hub -maxSession 50 -Dwebdriver.firefox.profile=Selenium

I launch one or more nodes using a batch file containing the following (each node has a unique port number):

"C:\Program Files (x86)\Java\jre6\bin\java.exe" -jar selenium-server-standalone-2.20.0.jar -role node -hub http://127.0.0.1:4444/grid/register -browser browserName=firefox,platform=WINDOWS,version=11.0,maxInstances=2 -maxSession 2 -port 5555 -Dwebdriver.firefox.profile=Selenium

The key here is the last part of those commands, which needs to match the name of the custom profile you have created.

Code to create WebDriver instance

private readonly Uri _remoteWebDriverDefaultUri = new Uri("http://localhost:4444/wd/hub/");

private IWebDriver CreateFireFoxWebDriver(Uri remoteWebDriverUri)
{
    var desiredCapabilities = new DesiredCapabilities();

    desiredCapabilities.SetCapability(CapabilityType.BrowserName, "firefox");
    desiredCapabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
    desiredCapabilities.SetCapability(CapabilityType.Version, "11.0");

    var drv = new RemoteWebDriver(remoteWebDriverUri ?? _remoteWebDriverDefaultUri, desiredCapabilities);

    return drv;
}

NOTE: The capabilities need to match those of the nodes you are running in the grid.

You can then call this method passing in the Uri of the hub, or null to default to localhost.


Seems fine with the Roaming profile rather than the local profile.

string path = @"C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles\myi5go1k.default"; FirefoxProfile ffprofile = new FirefoxProfile(path); Driver = new FirefoxDriver(ffprofile);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号