我有一个自动化要求,在某个步骤之后需要下载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");
使用所需首选项设置个人资料后。根据使用新的Marionette驱动程序
的要求初始化Firefox驱动程序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之前,代码工作正常。任何正确方向的建议或指示都会有很大的帮助。