C#:Firefox webdriver:如何将文件下载到特定文件夹

时间:2016-12-26 13:02:23

标签: c# selenium firefox

美好的一天。

我尝试为firefox webdriver设置选项,以便下载到特定的foler。我把我的代码放在下面。它在最后一行抛出了TypeInitializationException。

如何为文件下载设置默认文件夹并设置它应该没有警报窗口?

var firefoxOptions = new FirefoxOptions();
firefoxOptions.AddAdditionalCapability("browser.download.folderList", 2);          firefoxOptions.AddAdditionalCapability("browser.download.manager.showWhenStarting", false);
firefoxOptions.AddAdditionalCapability("browser.download.dir", Constants.DownloadFilesLocation);
firefoxOptions.AddAdditionalCapability("browser.helperApps.neverAsk.saveToDisk", "image/jpeg");
firefoxOptions.AddAdditionalCapability("browser.helperApps.neverAsk.saveToDisk", "image/png");
firefoxOptions.AddAdditionalCapability("browser.helperApps.neverAsk.saveToDisk", "image/gif");
firefoxOptions.AddAdditionalCapability("browser.helperApps.neverAsk.saveToDisk", "image/bmp");
firefoxOptions.AddAdditionalCapability("browser.helperApps.neverAsk.saveToDisk", "image/tiff");
Driver = new FirefoxDriver(firefoxOptions);

1 个答案:

答案 0 :(得分:0)

我认为您应该将功能值定义为字符串。此外,browser.helperApps.neverAsk.saveToDisk应定义为以逗号分隔的列表:

var firefoxOptions = new FirefoxOptions();

firefoxOptions.AddAdditionalCapability("browser.download.folderList", "2");
firefoxOptions.AddAdditionalCapability("browser.download.manager.showWhenStarting", "false");
firefoxOptions.AddAdditionalCapability("browser.download.dir", Constants.DownloadFilesLocation);
firefoxOptions.AddAdditionalCapability("browser.helperApps.neverAsk.saveToDisk", "image/jpeg, image/png, image/gif, image/bmp, image/tiff");

Driver = new FirefoxDriver(firefoxOptions);

另外,请检查Constants.DownloadFilesLocation是否为下载目录路径字符串。