car*_*rol 7 java arrays string selenium file
我有这个代码
File folder = new File("F:\\gals");
File[] listOfFiles = folder.listFiles();  
Run Code Online (Sandbox Code Playgroud)
此代码返回文件夹F:\ gals中所有文件的位置数组,我尝试在selenium代码中使用此位置
driver.findElement(By.id(id1)).sendKeys(listOfFiles[1]);
Run Code Online (Sandbox Code Playgroud)
我看到错误
The method sendKeys(CharSequence...) in the type WebElement is not applicable for the arguments (File)
Run Code Online (Sandbox Code Playgroud)
所以我想我必须将listOfFiles []转换为String数组,请告诉我简单的方法.谢谢
Fre*_*ula 12
您不需要转换整个数组.只要打电话File的getAbsolutePath()方法:
driver.findElement(By.id(id1)).sendKeys(listOfFiles[1].getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)
但是如果你想转换整个数组,这里是Java 8的方法(由@RemigiusStalder简化):
String listOfPaths[] = Arrays.stream(listOfFiles).map(File::getAbsolutePath)
        .toArray(String[]::new);
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           2844 次  |  
        
|   最近记录:  |