【问题标题】:Download a zip file using Selenium WebDriver in Java - Download Popup is coming always使用 Java 中的 Selenium WebDriver 下载 zip 文件 - 下载弹出窗口总是来
【发布时间】:2015-01-18 18:05:32
【问题描述】:

我正在使用以下代码从网站下载文件,我正在使用 Firefox 32.0.3 和 Selenium jar 版本 - 2.43。

    FirefoxProfile  firefoxProfile = new FirefoxProfile();
    firefoxProfile.setPreference("browser.download.folderList",2);
    firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
    firefoxProfile.setPreference("browser.helperApps.alwaysAsk.force", false);
    firefoxProfile.setPreference("browser.download.dir","C:\\RDM_Files");
    firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip;application/octet-stream;application/x-zip;application/x-zip-compressed");
    firefoxProfile.setPreference("plugin.disable_full_page_plugin_for_types", "application/zip");

    WebDriver driver = new FirefoxDriver(firefoxProfile);

我检查了正在下载的文件的 MIME 类型是 application/zip。每次我尝试下载文件时,都会出现一个窗口,要求打开文件或保存文件。

我搜索了 Stackoverflow.com 并找到了与处理 .pdf 文件但不是 zip 文件相关的帖子。请帮忙

【问题讨论】:

    标签: java selenium automation


    【解决方案1】:

    我认为您正在寻找类似的东西

    //common to all the cases
    FirefoxProfile prof = new FirefoxProfile();
    
    //Case:1 - Use this case to set download this code to your browser's default location
    //prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
    
    //Case:2 - Download file to Desktop
    //prof.setPreference("browser.download.folderList", 0);
    //prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
    
    //Case:3 - Download to custom folder path. Replace d:\\selenium with your Download Location 
    prof.setPreference("browser.download.dir","D:\\selenium\\");
    prof.setPreference("browser.download.folderList", 2);
    prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");
    
    //This will work for all cases mentioned above
    WebDriver driver = new FirefoxDriver(prof);
    driver.get("http://docs.seleniumhq.org/download/");
    driver.findElement(By.xpath("//tr[1]/td[4]/a[text()='Download']")).click();
    

    【讨论】:

      【解决方案2】:

      禁止弹出系统非网页下载/保存对话框。

      FirefoxProfile prof = new FirefoxProfile();
      
      ffprofile.setPreference("browser.download.panel.shown", false);
      ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
      
      //ffprofile.setPreference("browser.download.folderList", 1);  // Default to /home/user/Downloads in Linux.
      ffprofile.setPreference("browser.download.folderList", 2); 
      ffprofile.setPreference("browser.download.dir", "/tmp");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-09
        • 2016-07-21
        • 2019-03-25
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        • 2017-03-10
        相关资源
        最近更新 更多