在C#中使用Selenium WebDriver中的特定Firefox配置文件

我正在尝试使用我已经为seflium 2设置的firefox配置文件,但没有C#的文档。 我试过的代码如下:

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

我在Java中看到的代码使用的是使用ProfilesIni而不是FirefoxProfileManager的代码,但这在C#中不可用。 以这种方式设置驱动程序时,使用的selenium配置文件具有所有默认设置,而不是我试图指向的配置文件中指定的设置。

我不确定我是否使用正确的方法来检索配置文件,但如果有人使用Selenium 2和C#,任何信息都会有所帮助。

我们使用这种方法加载默认的firefox配置文件(您可以创建自定义配置文件并加载它):

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

我们遇到的问题是配置文件无法加载。 问题出在FirefoxProfile(第137行)。 它只查找user.js,Firefox中的配置文件实际上是prefs.js

137 >>文件prefsInModel = new File(model,“user.js”);

黑客解决方案:重命名prefs.js – > user.js

以下对我有用。 我必须专门设置“webdriver.firefox.profile”首选项才能使其正常工作。

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

我有同样的问题,它不是重复的。

我使用以下工作

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

我也遇到了同样的问题,在搜索并尝试了许多不同的组合之后,我能够在使用RemoteWebDriver时让Selenium加载特定的配置文件。

网格配置

我使用包含以下内容的批处理文件启动HUB

 "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 

我使用包含以下内容的批处理文件启动一个或多个节点(每个节点都有一个唯一的端口号):

 "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 

这里的关键是这些命令的最后一部分,它需要与您创建的自定义配置文件的名称相匹配。

用于创建WebDriver实例的代码

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

注意:function需要与您在网格中运行的节点的function相匹配。

然后,您可以调用此方法传入集线器的Uri,或者将null调用为默认为localhost。

使用漫游配置文件而不是本地配置文件似乎很好。

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