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

Selenium webdriver 如何使用SeleniumWebDriver处理文件下载弹出窗口?,selenium-webdriver,Selenium Webdriver,我已经写了一个代码来下载一个excel文件,它正在工作,但一旦弹出窗口出现,它就会停止。它会自动下载文件并存储在指定的位置,但现在不会发生这种情况。请任何人帮助找到这个问题的解决方案 FirefoxProfile profile = new FirefoxProfile(); WebDriver driver = new FirefoxDriver(profile); profile.setPreference("browser.helperapps.neverAsk.saveToDisk

我已经写了一个代码来下载一个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();

对于不提示下载对话框的首选项设置,请将
文本
替换为
应用程序
,如下所示:

profile.setPreference("browser.helperapps.neverAsk.saveToDisk" , "application/csv");
只是为了确认,下载的文件的扩展名是
csv
,对吗?如果不是这样,我们必须对上述代码进行更改。

尝试使用以下代码:

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"); 
试试下面的代码

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类型列表,请点击链接:

您是否尝试切换到出现并接受它的
弹出窗口
警报窗口
?不需要切换到该文件中的警报下载弹出窗口。它是excel文件,不是csv文件。它的扩展名是什么。xl、.xls、.xlt或其他什么?你能回答我上面的评论吗?然后尝试使用
profile.setPreference(“browser.helperapps.neverAsk.saveToDisk”,“application/excel”)
profile.setPreference(“browser.helperapps.neverAsk.saveToDisk”,“application/vnd.ms excel”)
profile.setPreference(“browser.helperapps.neverAsk.saveToDisk”,“application/x-excel”)
profile.setPreference(“browser.helperapps.neverAsk.saveToDisk”,“application/x-msexcel”)如果这对你有效,让我知道我会用这个更新我的答案。我会尝试让你知道这不会发生,和以前的结果一样,我必须强制单击“确定”按钮才能下载它不是csv文件,它是一个带有.xls扩展名Anks Hemasndar的excel,即使使用Firefox Quantum,它也可以正常工作!