[英]How to download and save excel and pdf file from pop-up in a desired location using Selenium written in C#
我想知道是否有任何方法可以使自動化測試下載文件(在我的情況下為Excel和pdf)並使用Selenium Web驅動程序保存在所需的位置。 我嘗試使用Firefox配置文件,但這沒有用。 測試運行時,會彈出一個窗口,詢問您是否打開或保存文件。 因此,當我們單擊一個按鈕時,我不希望顯示窗口彈出窗口,而是自動允許它在所需位置(本地和Selenium服務器上)下載。我們正在使用C#編寫測試。 附帶的是窗口彈出窗口。 有人可以幫忙嗎?
public static IWebDriver Build(SeleniumInstanceContext context)
{
IWebDriver instance;
var capabilities = new DesiredCapabilities();
var profile = CreateFirefoxProfile();
//Pass the Firefox profile to be used by RemoteWebDriver
capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());
switch (context.TestingBrowser.ToUpperInvariant())
{
case "CHROME":
instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
? new RemoteWebDriver(new Uri(context.SeleniumServerUrl), DesiredCapabilities.Chrome())
: new ChromeDriver();
break;
case "IE":
instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
? new RemoteWebDriver(new Uri(context.SeleniumServerUrl),
DesiredCapabilities.InternetExplorer())
: new InternetExplorerDriver();
break;
default:
instance = context.SeleniumEnvironment.ToUpper() == "REMOTEWEBDRIVER"
? new RemoteWebDriver(new Uri(context.SeleniumServerUrl), capabilities,
TimeSpan.FromMinutes(5))
: new FirefoxDriver(profile);
break;
}
return instance;
}
[![enter image description here][1]][1]public static FirefoxProfile CreateFirefoxProfile()
{
//Create FireFox Profile object
var profile = new FirefoxProfile();
//Set Location to store files after downloading.
const string path = "C:\\Users\\abc.xyz\\Downloads";
profile.SetPreference("browser.download.dir", path);
profile.SetPreference("browser.download.folderList", 2);
//Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel,application/csv");
//profile.SetPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream");
//profile.SetPreference("browser.download.manager.showWhenStarting", false);
profile.SetPreference("pdfjs.disabled", true);
profile.SetPreference("browser.download.alertOnEXEOpen", false);
profile.SetPreference("browser.download.manager.focusWhenStarting", false);
profile.SetPreference("browser.helperApps.alwaysAsk.force", true);
profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
profile.SetPreference("browser.download.manager.closeWhenDone", false);
profile.SetPreference("browser.download.manager.showAlertOnComplete", false);
profile.SetPreference("browser.download.manager.useWindow", false);
return profile;
}
下次當您使用此配置文件在自動測試中打開Firefox時,這些xls文件將被下載,沒有任何彈出窗口
看來您已經忘記在browser.helperApps.neverAsk.saveToDisk
首選項中包含一些MIME類型。
這是.xls
, .xlsx
和.pdf
文件擴展名的MIME類型:
.xls
application/excel
.xls
- application/vnd.ms-excel
.xls
application/x-excel
.xls
application/x-msexcel
.xlsx
- application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.pdf
application/pdf
參考文獻:
要為.xls
, .xlsx
和.pdf
文件擴展名隱藏此彈出窗口,請添加MIME類型,並用逗號分隔:
string[] mimeTypes = new string[]
{
// .xls
"application/excel",
"application/vnd.ms-excel",
"application/x-excel",
"application/x-msexcel",
// .xlsx
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
// .pdf
"application/pdf"
};
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk",
string.Join(",", mimeTypes));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.