Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法通过使用Selenium Webdriver设置Firefox配置文件下载文件_Java_Selenium_Selenium Webdriver - Fatal编程技术网

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

Java 无法通过使用Selenium Webdriver设置Firefox配置文件下载文件,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我想使用selenium处理文件下载。为此,我使用以下代码设置firefox配置文件: FirefoxProfile profile = new FirefoxProfile(); profile.setPreference("browser.download.folderList", 2); profile.setPreference("browser.download.manager.showWhenStarting", false); profile.setPref

我想使用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);
通过我的UI,我下载了2个文件。 第一个文件,我可以成功下载,其弹出窗口如下:

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

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

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

使用,您可以尝试以下内容,然后将其与您正在做的内容进行比较:

@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"));
    }

第一个文件的格式似乎是Microsoft office excel 97-2003工作表,第二个文件的格式是Microsoft office excel工作表(较新版本),因此它找不到打开的应用程序。请单击“保存”尝试保存它,然后试着打开。@Naruto我相信这确实不容易做到(甚至不可能)(单击保存)-仅使用硒。是否要求使用硒?或者我可以建议另一种方法?@Sammi您能告诉我们,单击一个下载按钮是否会弹出两个下载实例,或者您需要一个接一个地单击按钮以获得这两个实例。。。。。通过单击一个下载链接,代码看起来好像出现了两个弹出窗口。。。。