我有一个自动化要求,在某个步骤后,需要下载 csv 文件并将其保存到文件夹中。为了达到同样的效果,Firefox 驱动程序已设置如下。
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.download.folderList", 2);
try
{
profile.SetPreference("browser.download.manager.showWhenStarting", false);
}
catch
{
}
profile.SetPreference("browser.download.dir", Config.I.TempDirectory);
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
使用所需的首选项设置配置文件后。Firefox 驱动程序已根据使用新 Marionette 驱动程序的要求进行了初始化
public static RemoteWebDriver GetFirefoxDriver(FirefoxProfile profile = null)
{
FirefoxDriverService firefoxService = FirefoxDriverService.CreateDefaultService();
// this should be moved to a config file.
firefoxService.FirefoxBinaryPath = Config.I.FirefoxBinaryPath;
if (profile == null)
{
var firefoxOptions = new FirefoxProfileOptions() { IsMarionette = true };
return new FirefoxDriver(firefoxService, firefoxOptions, TimeSpan.FromSeconds(20));
}
else
{
var firefoxOption = new FirefoxProfileOptions(profile) { IsMarionette = true };
var driver = new FirefoxDriver(firefoxService, firefoxOption, TimeSpan.FromSeconds(30));
return driver;
}
}
/// <summary>
/// Extending the firefox options, so that both profile and service can be set for driver at the time of initialization,
/// Need to check whether the same needs to be here or not.
/// </summary>
public class FirefoxProfileOptions : FirefoxOptions
{
private DesiredCapabilities _capabilities;
public FirefoxProfileOptions()
: base()
{
_capabilities = DesiredCapabilities.Firefox();
_capabilities.SetCapability("marionette", this.IsMarionette);
}
public FirefoxProfileOptions(FirefoxProfile profile)
: this()
{
_capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
}
public override void AddAdditionalCapability(string capabilityName, object capabilityValue)
{
_capabilities.SetCapability(capabilityName, capabilityValue);
}
}
在我们升级到 selenium 2.53.1 和 Firefox 48 之前,代码运行正常。任何正确方向的建议或指示都会有很大帮助。