2016-12-26 150 views
0

美好的一天。C#:Firefox webdriver:如何將文件下載到特定文件夾

我嘗試設置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); 

回答

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是一個下載目錄路徑字符串。

+0

對於Chrome路徑接受並正常工作。我使用FirefoxProfile.SetPreference,但它也不起作用。文件仍下載到'C:\ Users \ User \ AppData \ Local \ Temp' – Evgeniy175

相關問題