java - 无法通过使用 Selenium Webdriver 设置 Firefox 配置文件来下载文件

标签 java selenium selenium-webdriver

我想使用 selenium 处理文件下载。为此,我使用以下代码来设置 firefox 配置文件:

FirefoxProfile profile = new FirefoxProfile();
    profile.setPreference("browser.download.folderList", 2);
    profile.setPreference("browser.download.manager.showWhenStarting", false);
    profile.setPreference("browser.download.dir", downloadPath);
    profile.setPreference("browser.helperApps.neverAsk.openFile",
            "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
   "text/csv,application/x-msexcel,application/excel,application/x-excel,application/vnd.ms-excel,image/png,image/jpeg,text/html,text/plain,application/msword,application/xml");
    profile.setPreference("browser.helperApps.alwaysAsk.force", false);
    profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
    profile.setPreference("browser.download.manager.focusWhenStarting", false);
    profile.setPreference("browser.download.manager.useWindow", false);
    profile.setPreference("browser.download.manager.showAlertOnComplete", false);
    profile.setPreference("browser.download.manager.closeWhenDone", false);

通过我的用户界面,我正在下载 2 个文件。 第一个文件,我可以成功下载它的弹出窗口是这样的:

enter image description here

但是我无法下载第二个文件。对于第二个文件,弹出窗口如下所示: enter image description here

我不确定为什么我的 firefox 配置文件设置无法处理第二个文件的下载。

请建议。任何帮助将不胜感激!!

最佳答案

使用 Selenide ,您可以尝试这样的事情,然后将其与您正在做的事情进行比较:

@Test
    public void userCanDownloadFile() throws FileNotFoundException, IOException
    {
     // Folder to store downloads and screenshots to.
     reportsFolder = "./src/test/profiles/chrome/downloads/";

     open("http://chromedriver.storage.googleapis.com/index.html?path=2.16/");

     // Download files
     $("a[href='/2.16/chromedriver_win32.zip']").download();
        $(By.xpath(".//a[@href='/2.16/chromedriver_mac32.zip']")).download();

        // Count files in folder, assert 2
        int downloadsCount = new File(reportsFolder+"2.16").listFiles().length;
        assertEquals("Should be 2 files but founded " + downloadsCount,  
              downloadsCount, 2); 

        // Clean after test
        FileUtils.deleteDirectory(new File(reportsFolder+"2.16"));
    }

关于java - 无法通过使用 Selenium Webdriver 设置 Firefox 配置文件来下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34401417/

相关文章:

java - 为什么 ByteArrayOutputStream 使用 int?

java - 一台服务器支持多个客户端 Java

java - Selenium 在 Firefox 配置文件中禁用插件

java - 验证selenium中标签的值

c# - 使用 [FindsBy] 属性时将 IWebElements 作为 C#/Selenium 中的集合保存

java - 我们可以在 TestNG 中将两个注释(IRetryAnalyzer 和优先级)类型作为一个类型传递吗

java - Swing Jbutton : showing border and background only when it is hovered

javascript - 菱形方 block 算法不起作用(将代码从 JS 重写为 JAVA)

java - Selenium 找不到输入类型提交

google-chrome - 为什么我必须点击两次才能使用 selenium 提交输入