如何使用Selenium WebDriver处理文件下载弹出窗口?

时间:2014-05-05 06:26:47

标签: selenium-webdriver

我已经编写了一个代码来下载excel文件,它正在运行,但是一旦出现弹出窗口就会停止。它应该自动下载文件并存储在指定的位置,这是现在没有发生的。请任何人帮助找到解决此问题的方法

FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile); 


profile.setPreference("browser.helperapps.neverAsk.saveToDisk" , "text/csv"); 
profile.setPreference("browser.download.folderList", 2); 
profile.setPreference("browser.download.dir","e:\\SampleExcel"); 

driver.get("http://url");
driver.findElement(By.name("email")).sendKeys("abc@gmail.com");
driver.findElement(By.name("pass")).sendKeys("abc");
driver.findElement(By.id("edit-submit")).click();
driver.findElement(By.id("toolbar-link-admin-config")).click();
driver.findElement(By.linkText("Reports")).click();
driver.findElement(By.xpath("//input[@value='5']")).click();
driver.findElement(By.id("edit-submit")).click();

3 个答案:

答案 0 :(得分:5)

尝试以下代码

FirefoxProfile profile = new FirefoxProfile();
        String path = "D:\\Downloads_sel";
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", path);
        profile.setPreference("browser.download.alertOnEXEOpen", false);
        profile.setPreference("browser.helperApps.neverAsksaveToDisk", "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");
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        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);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
        profile.setPreference("pdfjs.disabled", true);

        WebDriver driver = new FirefoxDriver(profile);

有关完整的MIME类型列表,请点击以下链接:http://qaautomationworld.blogspot.in/2014/02/file-downlaoding-using-selenium.html

答案 1 :(得分:4)

尝试使用此代码:

FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile); 

profile.setPreference("browser.helperApps.neverAsk.saveToDisk" , "application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;"); 
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.folderList", 2); 
profile.setPreference("browser.download.dir","e:\\SampleExcel"); 

答案 2 :(得分:0)

请将text替换为application,以获取不提示下载对话框的首选项设置,如下所示:

profile.setPreference("browser.helperapps.neverAsk.saveToDisk" , "application/csv");

只是为了确认,下载的文件的扩展名为csv,对吗?如果不是这样,我们将不得不在上面的代码中进行更改。