Access to file download dialog in Firefox

前端 未结 11 2603
一个人的身影
一个人的身影 2020-11-22 07:02

Is there any kind of API that can allow me to manipulate a file download dialog in Firefox? (I want to access the one that appears when user does something, not initiate one

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 07:46

    Web Applications generate 3 different types of pop-ups; namely,

     1| JavaScript PopUps
     2| Browser PopUps
     3| Native OS PopUps [e.g., Windows Popup like Upload/Download]
    

    In General, the JavaScript pop-ups are generated by the web application code. Selenium provides an API to handle these JavaScript pop-ups, such as Alert.

    Eventually, the simplest way to ignore Browser pop-up and download files is done by making use of Browser profiles; There are couple of ways to do this:

    • Manually involve changes on browser properties (or)
    • Customize browser properties using profile setPreference

    Method1

    Before you start working with pop-ups on Browser profiles, make sure that the Download options are set default to Save File.

    (Open Firefox) Tools > Options > Applications

    enter image description here

    Method2

    Make use of the below snippet and do edits whenever necessary.

    FirefoxProfile profile = new FirefoxProfile();
    
    String path = "C:\\Test\\";
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.dir", path);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.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");
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    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.alertOnEXEOpen", 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);
           
    driver = new FirefoxDriver(profile);
    

提交回复
热议问题