I would like to know if there is any way for the automated tests to download the file (excel and pdf in my case) and save in a desired location using selenium web driver. I tried using Firefox profile, but that didn't work. When the test is running, there is window pop-up with asking whether to open or save the file. So, when we click a button, I do not want the windows pop-up to display, instead automatically allow it to download in a desired location( both locally and on Selenium Server) We are using C# to write the tests. Attached is the windows pop-up. Could someone please help with this?
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;
}
Next time you open Firefox with this profile in autotests these xls files will be downloaded without any pop-up window
It appears that you've forgotten to include some MIME types in the browser.helperApps.neverAsk.saveToDisk
preference.
Here are the MIME types for .xls
, .xlsx
, and .pdf
file extensions:
.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
References:
To hide this pop up for .xls
, .xlsx
, and .pdf
file extensions, add the MIME types, separated by a comma:
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.