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

15

我正在尝试使用已经设置好的Firefox配置文件来使用selenium 2,但是没有C#的文档。我尝试过的代码如下:

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

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

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


可能是这个帖子的重复:http://stackoverflow.com/questions/2250486/how-to-set-firefox-profile-selenium-rc-net-client-driver - linguini
2
这不是重复的主题,另一个主题涉及Selenium RC和无法启动浏览器。 - Simon
你最终找到解决方案了吗? - TWilly
7个回答

7
我们使用以下方法来加载默认的 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();
}

哪个版本的Selenium?对于2.23.0,什么都不起作用,甚至这个也不行。 - mishap
完全有道理。感谢您的回应。 - mishap
这段代码对我来说可以工作,只需要进行一些小的调整。谢谢。 - Cheyne
string pathToCurrentUserProfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Comodo\IceDragon\Profiles"); - Vojtěch Dohnal

3
我们遇到了同样的问题,无法加载配置文件。问题出在FirefoxProfile(第137行)。它只查找user.js文件,而Firefox的配置文件实际上是prefs.js。
137>> File prefsInModel = new File(model, "user.js");
临时解决方案:将prefs.js重命名为user.js。

对我来说有效,只需要确保你在实际的个人资料文件夹中,不要压缩文件夹本身。 - Saulius Antanavicius

1

在尝试了上述答案后,我没有得到任何结果,所以我尝试了以下替代方法:

首先,在Firefox地址栏中键入about:profiles来创建所需的配置文件。

其次,是C#代码。请注意,我们在第一步创建的配置文件名称将作为参数传递。

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

1
以下方法适用于我。我必须具体设置"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);

0

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

我正在使用以下代码,它可以正常工作

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

1
即使我在默认位置创建了一个配置文件,Selenium webdriver仍然坚持使用新的配置文件。我知道路径是正确的,但webdriver拒绝使用它。该配置文件很好,我可以通过配置文件管理器启动Firefox。困惑!! - Simon
出于绝望,我更新了Selenium,但没有用,所以我更新了FireFox,仍然没有成功。有人知道是否可以使用SetPreference方法做些什么吗? - Simon
同样的问题...即使使用了 profile.SetPreference("webdriver.firefox.profile", "SeleniumUser");,它也不会使用 SeleniumUser 的任何历史记录或保存的密码。 - Brackets

-1

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

网格配置

我使用包含以下内容的批处理文件启动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;
}

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

然后,您可以调用此方法,传入hub的Uri,或将其设置为null以使用默认的本地主机。


-1

使用漫游配置文件似乎比使用本地配置文件更好。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接