繁体   English   中英

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

[英]Using a specific Firefox profile in Selenium WebDriver in C#

我正在尝试使用我已经为带有 selenium 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 =新文件(模型,“user.js”);

Hack 解决方案:重命名 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);

使用上述答案后,我没有结果,所以我尝试了以下替代方法:

首先,通过在 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);                        
                    
    }
}

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

我正在使用以下有效的

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

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

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

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

string path = @"C:\\Users\\username\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\myi5go1k.default"; FirefoxProfile ffprofile = 新的 FirefoxProfile(path); 驱动程序 = 新的 FirefoxDriver(ffprofile);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM