2017-06-12 68 views
0

我目前正在使用Selenium从我们校园周围的几十台MFD打印机中提取一些数据,并且它大部分都很顺利。我可以登录,点击菜单,并下载文件我设置的位置。我需要做的是依次登录到每台打印机并将相同的文件下载到不同的文件夹。到目前为止,相关的代码如下所示:使用Selenium和Java将多个文件下载到多个位置

chromePrefs.put("profile.default_content_settings.popups", 0); 
    chromePrefs.put("download.default_directory", downloadFilepath); 

    HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>(); 
    options.setExperimentalOption("prefs", chromePrefs); 
    options.addArguments("--test-type"); 

    cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap); 
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); 
    cap.setCapability(ChromeOptions.CAPABILITY, options); 

    webDriver = new ChromeDriver(cap); 
    wait = new WebDriverWait(webDriver, 20); 
    String fileName = "Y:/MFDAutomationEnv/test_mfd_list.txt"; 

    readFile(fileName); 
    if(fileData == null){ 
     logPrint("Couldn't read from " + fileName); 
     return; 
    } 

    for(int currentMFD = 0; currentMFD < fileData.length; currentMFD++){ 
     //navigate 
     mfdDownloadButton(ip); 
     mfdLogout(ip); 
    } 

private static int mfdDownloadButton(String ip){ 
    try{ 

     chromePrefs.put("download.default_directory", downloadFilepath + "someSubDirectory/"); 
wait.until(ExpectedConditions.elementToBeClickable(By.id("btnEXE"))).click(); 
    } 
    catch(Exception e){ 
     logPrint("Failed to find download button for " + ip); 
     return 1; 
    } 
    return 0; 
} 

因此,基本上,有没有办法来改变在运行中下载位置,而无需重新实例一个全新的webdriver?

回答

0

你在做什么是配置你的浏览器自动下载文件到指定的位置。还有另外一种方法可以使用。导航到下载文件按钮所在的页面后,您是否可以抓取文件的URL?如果是,您可以使用Apache HttpClient下载文件。 Here是关于它的好文章。