我有这个代码
File folder = new File("F:\\gals");
File[] listOfFiles = folder.listFiles();
此代码返回文件夹 F:\gals 中所有文件的位置数组,我尝试在 selenium 代码中使用此位置
driver.findElement(By.id(id1)).sendKeys(listOfFiles[1]);
我看到错误
The method sendKeys(CharSequence...) in the type WebElement is not applicable for the arguments (File)
所以我想我必须将 listOfFiles[] 转换为字符串数组,请告诉我简单的方法来做到这一点。谢谢
最佳答案
您不需要转换整个数组。只需调用 File
的 getAbsolutePath()
方法:
driver.findElement(By.id(id1)).sendKeys(listOfFiles[1].getAbsolutePath());
但是如果你确实想转换整个数组,这里是 Java 8 的方法(由@RemigiusStalder 简化):
String listOfPaths[] = Arrays.stream(listOfFiles).map(File::getAbsolutePath)
.toArray(String[]::new);
关于java - 在 Java 中将 File[] 转换为 String[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34652515/