Java 如何使用selenium在excel文件中查找字符串的位置
案例:我的字符串位置是动态的,因此我必须在excel中搜索字符串位置,然后获取该位置旁边的值 这就是我尝试过的Java 如何使用selenium在excel文件中查找字符串的位置,java,excel,data-driven-tests,Java,Excel,Data Driven Tests,案例:我的字符串位置是动态的,因此我必须在excel中搜索字符串位置,然后获取该位置旁边的值 这就是我尝试过的 public class AssetRegisterdepreciationReport { public static void ReadAssetExcelData() throws IOException { String cellContent = "Grand Total:"; int rownr, col
public class AssetRegisterdepreciationReport
{
public static void ReadAssetExcelData() throws IOException
{
String cellContent = "Grand Total:";
int rownr, colnr = 5;
FileInputStream AssetReport = new FileInputStream("C:\\Users\\Admin\\Downloads\\Asset_Depreciation_14_03_2020 19-05-31.xlsx");
@SuppressWarnings("resource")
XSSFWorkbook Assetworkbook = new XSSFWorkbook(AssetReport);
XSSFSheet sheetnumber1 = Assetworkbook.getSheetAt(0);
rownr = findRow(sheetnumber1, cellContent);
output(sheetnumber1, rownr, colnr);
finish();
}
private static void output(XSSFSheet sheetnumber1, int rownr, int colnr)
{
XSSFRow row = sheetnumber1.getRow(rownr);
XSSFCell cell = row.getCell(colnr);
System.out.println("Your total is: " + cell);
}
private static int findRow(XSSFSheet sheetnumber1, String cellContent)
{
int rowNum = 0;
for(Row row : sheetnumber1)
{
for(Cell cell : row)
{
switch(cell.getCellType())
{
case STRING:
if(cell.getRichStringCellValue().getString() == cellContent)
{
rowNum = row.getRowNum();
System.out.println(rowNum);
}
default:
break;
}
}
}
return rowNum;
}
private static void finish()
{
System.exit(0);
}
}
findrow()未返回任何rownum值(返回默认值)。对于Java中的字符串比较,请使用.equals()而不是==
if(cell.getRichStringCellValue().getString().equals(cellContent))
这个问题与硒无关,如果我不得不猜测的话,我会说您正在使用ApachePOI。我建议您更新您的问题以去除硒。