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
使用Selenium和Java将多个文件下载到多个位置_Java_Selenium - Fatal编程技术网

使用Selenium和Java将多个文件下载到多个位置

使用Selenium和Java将多个文件下载到多个位置,java,selenium,Java,Selenium,我目前正在使用Selenium从校园内的几十台MFD打印机中提取一些数据,而且大部分进展顺利。我可以登录,单击菜单,然后将文件下载到我设置的位置。不过,我需要做的是依次登录到每台打印机,并将相同的文件下载到不同的文件夹中。到目前为止,相关代码如下所示: chromePrefs.put("profile.default_content_settings.popups", 0); chromePrefs.put("download.default_directory", downlo

我目前正在使用Selenium从校园内的几十台MFD打印机中提取一些数据,而且大部分进展顺利。我可以登录,单击菜单,然后将文件下载到我设置的位置。不过,我需要做的是依次登录到每台打印机,并将相同的文件下载到不同的文件夹中。到目前为止,相关代码如下所示:

    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;
}
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
chromePrefs.put(“download.default\u目录”,downloadFilepath);
HashMap chromeOptionsMap=新HashMap();
选项。设置实验选项(“prefs”,chromePrefs);
options.addArguments(“--test type”);
cap.setCapability(ChromeOptions.CAPABILITY,ChromeOptions sMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
webDriver=新的ChromeDriver(cap);
wait=新的WebDriverWait(webDriver,20);
字符串fileName=“Y:/MFDAutomationEnv/test\u mfd\u list.txt”;
读取文件(文件名);
if(fileData==null){
日志打印(“无法从“+文件名”读取);
返回;
}
对于(int currentMFD=0;currentMFD

因此,基本上,有没有一种方法可以在不重新启动全新的webdriver的情况下动态更改下载位置?

您所做的是将浏览器配置为自动将文件下载到指定位置。您还可以使用另一种方法。导航到下载文件的按钮所在的页面后,您可以获取文件的URL吗?如果是,您可以使用ApacheHttpClient下载该文件。这是一篇关于它的好文章