Selenium C# - 设置配置文件路径



我想使用存储在文件夹中的特定 Firefox 配置文件。

到目前为止我的代码:

        IWebDriver driver;
        string pathToCurrentUserProfiles = @"C:FirefoxProfile";
        string[] pathsToProfiles = Directory.GetDirectories(pathToCurrentUserProfiles, "*.default");
        FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]);
        driver = new FirefoxDriver(profile);
        driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
        driver.Navigate().GoToUrl("https://localhost");

Visual Studio虽然向我显示了这个错误:

Error   CS1503  Argument 1: cannot convert from 'OpenQA.Selenium.Firefox.FirefoxProfile' to 'OpenQA.Selenium.Firefox.FirefoxOptions'

你需要创建一个 FirefoxOptions 对象并设置 Profile 属性:

FirefoxProfile profile = new FirefoxProfile(pathsToProfiles[0]);
FirefoxOptions options = new FirefoxOptions()
{
    Profile = profile
};
driver = new FirefoxDriver(options);

您可以通过查看 FirefoxDriver 构造函数接受的参数来推断此解决方案。

最新更新