Handle download confirmation popup/dialog in Firefox - selenium

I have a test to upload ,compress and download pdf files. Everything is working fine except the download part. I am not able to get how to handle the confirmation popup/Save or Open file dialog in Firefox when we click on download. I tried modifying firefox config but still not able to solve it. Can anyone please help?
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.dir", "C:\\Users\\Sahil\\Downloads\\");
profile.setPreference("browser.helperApps.neverAsk.openFile","application/pdf");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
, 0 profile.setPreference("pdfjs.disabled", true);
WebDriver driver=new FirefoxDriver(profile);
driver.get("http://www.ilovepdf.com/compress_pdf");
driver.findElement(By.id("pickfiles")).click();
Runtime.getRuntime().exec("C:\\Users\\Sahil\\Documents\\Au\\Second.exe");
WebDriverWait wait=new WebDriverWait(driver, 100);
WebElement element1=wait.until(ExpectedConditions.elementToBeClickable(By.id("uploadfiles")));
element1.click();
WebElement element=wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("a#download")));
element.click();

You have a lot of preferences that you do not need. All you need is this:
// Create a firefoxprofile for firefox specific settings
FirefoxProfile profile = new FirefoxProfile();
// Set the downloads folder
profile.setPreference("browser.download.dir", path/to/folder);
// Download files to the downloads folder
profile.setPreference("browser.download.folderList", 2);
// Don't show downloads window when download starts
profile.setPreference("browser.download.manager.showWhenStarting", False);
// Prevent file download dialog to be shown for certain MIME-types
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
If this doesn't work you should make sure you have correct MIME-type specified. You can do this monitoring the network in your browser and download the file manually. A GET request should be made with a specific content-type. Make sure that content-type has been added to your browser.helperApps.neverAsk.saveToDisk preference.
Edit!
I just checked the MIME-type for the website you have specified. You are trying auto-saving a application/pdf. However you are downloading a file with MIME-type application/octet-stream.
You need to change this:
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
To this:
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");
How to find the MIME-type
Before you hit the download button open your firefox console and go to the network tab.
Now if you hit the download button you will see a GET request has been made. If you open this request you can see the content-type of the response. This is your MIME-type.

Related

Geckodriver Selenium Auto Download PDFs

I'm trying to automatically download .pdf files in geckodriver/Firefox. I've searched on stackoverflow and other resources and think the code below should work:
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", 'C:\\Users\\xyz\\Downloads\\')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf")
profile.set_preference("pref.downloads.disable_button.edit_actions", True)
profile.set_preference("browser.helperApps.neverAsk.openFile", "application/octet-stream,application/pdf,application/x-pdf,application/vnd.pdf")
browser = webdriver.Firefox(firefox_profile=profile)
I also tried:
profile.set_preference("pdfjs.disabled", True)
However, Firefox does not automatically download the .pdf (even though it is application/pdf in the http req). Also, after I load that profile in Firefox, under "Options / Applications", the PDF format still shows "Preview in Firefox" instead of "Save File"... What am I doing wrong?
Try by adding the following preference also, to the existing list:
fp.setPreference("pdfjs.enabledCache.state", false);
fp.setPreference("browser.helperApps.neverAsk.openFile","application/pdf");
To disable open and download pdf in firefox:
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.download.dir", pathToDownload);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
options.addPreference("pdfjs.enabledCache.state",false);
WebDriver driver = new FirefoxDriver(options);

Preferences set to avoid download dialog in firefox is not working for pdf

I have tried to set all possible preferences to avoid open and save file dialog while download files using selenium.
It is working for text files but not for PDF files
Below are the preferences set:
String downloadPath = <some random path>;
String mimetypes = "application/vnd.pdf,application/vnd.adobe.xfdf,text/csv,application/x-msexcel,application/excel,application/pdf,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml";
String url = "http://only-testing-blog.blogspot.in/2014/05/login.html";
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.panel.shown", false);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.dir", downloadPath);
profile.setPreference("browser.helperApps.neverAsk.openFile", mimetypes);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", mimetypes);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
FirefoxDriver driver = new FirefoxDriver(profile);
driver.get(url);
driver.findElement(By.xpath("//a[contains(.,'Download PDF File')]")).click();
Am using Firefox 46 with 2.53.0 selenium version
Please help me to make this to work for PDF, Excel and Word files as well
Thanks!
One solution I found for this is to use the ProfileIni class. It allows you to load a premade firefox profile. You use as shown below
private ProfilesIni init = new ProfilesIni();
private FirefoxProfile profile = init.getProfile("QA");
private WebDriver driver = new WebDriver(profile);
Then, you can set all of those preferences straight on the profile, and set the setting to skip the download dialogue and automatically save to the destination folder you want and keep the settings for every test case you need to download stuff on

Download Excel file from Firefox using Selenium [duplicate]

I am trying to download an Excel file from Firefox and Webdriver, but i can't handle the download pop-up.
When click on button i need the file to download automatically, without showing pop-up.
Here is my code:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.dir", Constant.Downloaded_Path);
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv/xls/xlsx");
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.setPreference("browser.download.manager.focusWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.useWindow", false);
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
firefoxProfile.setPreference("browser.download.manager.closeWhenDone", false);
return firefoxProfile;
But, the above code is not working. Can any one help?
First you need to get the mime type corresponding to the file:
Open Developer Tools and then the Network tab
Go back to the page and click on the file to download
Go back to the network panel and select the first request
Copy the mime type on the right of Content-Type from the response header:
Set the preference "browser.helperApps.neverAsk.saveToDisk" with your mime type
Make sure the download folder "browser.download.dir" exists
Here is a working example with Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Windows\\temp");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel");
profile.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer
WebDriver driver = new FirefoxDriver(profile);
driver.get("http://www.exinfm.com/free_spreadsheets.html");
driver.findElement(By.linkText("Capital Budgeting Analysis")).click();
FirefoxProfile profile = new FirefoxProfile();
// profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", " text/plain, application/octet-stream doc xls pdf txt");
profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/csv, text/csv, text/plain,application/octet-stream doc xls pdf txt");
profile.SetPreference("browser.download.manager.focusWhenStarting", false);
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.download.manager.closeWhenDone", true);
profile.SetPreference("browser.download.manager.showAlertOnComplete", false);
profile.SetPreference("browser.download.manager.useWindow", false);
profile.SetPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.SetPreference("pdfjs.disabled", true);
_driverInstance = new FirefoxDriver(profile);
These settings worked for me. Hope it might help you.
you need to provide mimetypes for the application you want to download.
For python, if you want to enable it for all applications then use the following code
import mimetypes
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", ','.join(list(it for it in mimetypes.types_map.values())))

Firefox download popup still persists after profile setting in Selenium

After setting up a profile for running tests in Firefox, I set the download popups to false but am still seeing it upon running my tests. Here is the profile I am setting up:
switch (browser){
case "FFX":
System.out.println("Starting test in FireFox");
try {
driver = new FirefoxDriver(firefoxProfile());
} catch (Exception e) {
e.printStackTrace();
}
//TODO Create a system properties file in case driver location moves.
break;
...
public static FirefoxProfile firefoxProfile() throws Exception {
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
firefoxProfile.setPreference("browser.download.manager.alertOnEXEOpen", false);
firefoxProfile.setPreference("browser.download.manager.focusWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.useWindow", false);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.closeWhenDone", false);
firefoxProfile.setPreference("browser.download.animateNotifications", false);
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.dir", downloadPath);
firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/"
+ "png,image/jpeg,text/html,text/plain,application/msword,application/xml");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/"
+ "png,image/jpeg,text/html,text/plain,application/msword,application/xml");
// ProfilesIni profile = new ProfilesIni();
// firefoxProfile = profile.getProfile("selenium");
return firefoxProfile;
But I am still getting this window:
Am I missing an entry for my profile settings? I thought it would be:
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
EDIT: I have added more to this, How I am setting up my driver and how I am building my profile. I also added the commented out part where I simply just assign a profile "selenium" to the driver. Currently what happens is the driver starts up as if it was just ran "out of the box" fresh install every single time. It only adheres to the setPreferences (or almost all of them) and completely ignores any custom profile I set up in advance. The ongoing download confirmation notification is really what is killing my tests here. Any ideas or observations are greatly appreciated.
Once you run the selenium code, you can manually check if the preference is getting set properly by typing 'about:config' in Firefox url bar. Also, try toggling browser.download.animateNotifications to false on the about:config page. Hope it helps.

How to disable 'Download Complete' notification using selenium Webdriver

I am using selenium to automate a functionality on my webpage.
I am downloading certain files using selenium. But as soon as a download completes, I can see a pop up notification on my web page that the download is complete.
enter image description here
Kindly suggest using selenium, I can I remove this pop up notification?
I am setting the following firefox profile:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.download.folderList", 2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
firefoxProfile.setPreference("browser.download.manager.useWindow", false);
firefoxProfile.setPreference("browser.download.manager.showAlertOnComplete", false);
firefoxProfile.setPreference("browser.download.manager.closeWhenDone", true);
firefoxProfile.setPreference("browser.download.dir", downlodeLocation);
firefoxProfile.setPreference("browser.helperApps.neverAsk.openFile",
"application/bin,application/binary,text/comma-separated-values,application/octet-stream,text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,text/html,text/plain,application/csv,text/anytext");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/bin,application/binary,text/comma-separated-values,application/octet-stream,text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,text/html,text/plain,application/csv,text/anytext");
Any help will be appreciable.
Thanks
You can do it via the browser settings
Type about:config in the address bar. Search for browser.download.animateNotifications or browser.download.manager.showAlertOnComplete and set them to false by right click -> toggle.