In this post we are going to handle the "Download popup dialog box" . when we are downloading files from Webpages.
Usually when we are downloading files from webpages. we get a popup like below.
We can't handle this popup using Selenium Webdriver.
To solve this issue in selenium we need to use FireFoxprofile to disable the popup dialog window.
In this Example:
- Setting Perference in the FireFoxProfile to disable the popup
String downloadPath = "C:\\mydownload\\";
FirefoxProfile
myprofile=new FirefoxProfile();
myprofile.setPreference("browser.download.folderList", 2);
myprofile.setPreference("browser.download.manager.showWhenStarting", false);
myprofile.setPreference("browser.download.dir", downloadPath);
myprofile.setPreference("browser.helperApps.neverAsk.openFile","application/msword,
application/csv, application/ris, text/csv, image/png, application/pdf,
text/html, text/plain, application/zip, application/x-zip,
application/x-zip-compressed, application/download,
application/octet-stream");
myprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword,
application/csv, application/ris, text/csv, image/png, application/pdf,
text/html, text/plain, application/zip, application/x-zip,
application/x-zip-compressed, application/download,
application/octet-stream");
myprofile.setPreference("browser.helperApps.alwaysAsk.force", false);
myprofile.setPreference("browser.download.manager.showAlertOnComplete", false);
myprofile.setPreference("browser.download.manager.closeWhenDone", false);
2. Set the Profile in FirefoxDriver.
driver=new
FirefoxDriver(myprofile);
3. Navigate to firefox official webpage to download the setup
driver.get("https://www.mozilla.org/en-US/firefox/new/");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='download-button-desktop-release']/ul/li[1]/a/span")).click();
Below See the full Source Code
Sourcecode:
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.firefox.FirefoxProfile;
public class DownloadProfile {
public static void main(String[] args) throws InterruptedException
{
// TODO Auto-generated
method stub
WebDriver
driver;
String
downloadPath = "C:\\mydownload\\";
FirefoxProfile
myprofile=new FirefoxProfile();
myprofile.setPreference("browser.download.folderList", 2);
myprofile.setPreference("browser.download.manager.showWhenStarting", false);
myprofile.setPreference("browser.download.dir", downloadPath);
myprofile.setPreference("browser.helperApps.neverAsk.openFile","application/msword,
application/csv, application/ris, text/csv, image/png, application/pdf,
text/html, text/plain, application/zip, application/x-zip,
application/x-zip-compressed, application/download,
application/octet-stream");
myprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword,
application/csv, application/ris, text/csv, image/png, application/pdf,
text/html, text/plain, application/zip, application/x-zip,
application/x-zip-compressed, application/download,
application/octet-stream");
myprofile.setPreference("browser.helperApps.alwaysAsk.force", false);
myprofile.setPreference("browser.download.manager.showAlertOnComplete", false);
myprofile.setPreference("browser.download.manager.closeWhenDone", false);
driver=new
FirefoxDriver(myprofile);
driver.get("https://www.mozilla.org/en-US/firefox/new/");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[@id='download-button-desktop-release']/ul/li[1]/a/span")).click();
System.out.println("Download done
without popup");
}
}
Run the project, Notice that Download Starts without popup the dialog window.
Thats it,
Thanks, Have a great day !!.