[英]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.