【问题标题】:how to read and validate blank lines from selenium excel如何从 selenium excel 中读取和验证空行
【发布时间】:2021-10-15 19:53:17
【问题描述】:

在我的代码中,我试图只读取“A”列中的数据。在屏幕截图中,我附上了示例数据的外观。由于空行,我在读取内容时遇到问题,因为它抛出空指针异常。我还需要验证空白行。请给我你的想法

  public void verifyAllSupportingLogs() throws Exception {
    Sheet sheet = getFilenameSupportingLogs("c:\\DataFile");
    int rowCount = sheet.getPhysicalNumberOfRows();
    XSSFCell firstColumnCell = null;
    int firstColumnRowCount = 0;
    ArrayList<String> InnerArray = new ArrayList<>();
    String cellValues = null;
    String GetsupportingLogs1 = null;
    for (int i = 0; i < rowCount; i++) {
      try {
        XSSFRow row = (XSSFRow) sheet.getRow(i);
        firstColumnCell = row.getCell(0);
      } catch (NullPointerException nullPointerException) {
        System.out.println("Cell is null at index: " + i);
      }
      if (firstColumnCell != null) {
        if (firstColumnCell.getStringCellValue().length() > 0) {
          firstColumnRowCount = i;

        }
      }
    }
    for (int j = 0; j <= firstColumnRowCount; j++) {
      XSSFRow row = (XSSFRow) sheet.getRow(j);
      XSSFCell cell = row.getCell(0);
      String valuesFromExcel = cell.getStringCellValue();
      InnerArray.add(valuesFromExcel);
      cellValues = InnerArray.toString();
    }
    String GetsupportingLogs = con.clickOnSupportingLogsTextbox(GetsupportingLogs1);
    System.out.println("GetsupportingLogs" + GetsupportingLogs);
    con.checkValueSupportingLogs(cellValues, GetsupportingLogs);
  }
  public void checkValueSupportingLogs(String GetsupportingLogs, String cellValue) throws Exception {
java.lang.String[] cellValue1 = cellValue.split("\n");
for (String cellValue2 : cellValue1) {
  if (GetsupportingLogs.contains(cellValue2)) {
    System.out.println("Success, this string is in the supporting logs.");
  } else {
    reportFailure("ERROR! This string is not in the supporting logs.");
  }
}

}

【问题讨论】:

  • @pburgr 对此有什么想法吗?
  • 迭代行...检查行是否为空...在此迭代中迭代单元格...检查单元格是否为空...使用 if/then... 在 C# 中就像“ if (row == null) continue; if (row.GetCell(j) != null) {...} POI 在工作表上也有获取第一行的功能。

标签: java excel selenium


【解决方案1】:

如果单元格为空,只需应用相同的检查。

代替:

for (int j = 0; j <= firstColumnRowCount; j++) {
  XSSFRow row = (XSSFRow) sheet.getRow(j);
  XSSFCell cell = row.getCell(0);
  String valuesFromExcel = cell.getStringCellValue();
  InnerArray.add(valuesFromExcel);
  cellValues = InnerArray.toString();
}

使用这个:

XSSFCell cell = null;
for (int j = 0; j <= firstColumnRowCount; j++) {
    XSSFRow row = (XSSFRow) sheet.getRow(j);
    try {
        cell = row.getCell(0);
    }
    catch (NullPointerException nullPointerException) {
        System.out.println("Cell is null at index: " + j);
    }
    if (cell != null) {
        String valuesFromExcel = cell.getStringCellValue();
        InnerArray.add(valuesFromExcel);
        cellValues = InnerArray.toString();
        cell = null;
    }
}

如果excel文件中的数据类型不同,可以结合这个:Error : Cannot get a STRING value from a NUMERIC cell in Selenium

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多