I have a table on the page and i need to read values from particular cell in the table. I want to write those values to excel file. Issue is that for loop loops only once and value is written only for first cell in excel. I have tried many different loops and searched on google but can't find an answer. Please help. Here is my code to write to excel:
public static void setExcelFile(String Path, String SheetName) throws Exception {
try {
// Open the Excel file
FileInputStream ExcelFile = new FileInputStream(Path);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
} catch (Exception e) {
throw (e);
}
}
This is where i am writing to excel file
public static void setCellData(String Result, int RowNum, int ColNum) throws Exception {
try {
Row = ExcelWSheet.getRow(RowNum);
Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);
if (Cell == null) {
Cell = Row.createCell(ColNum);
Cell.setCellValue(Result);
} else {
Cell.setCellValue(Result);
}
// Constant variables Test Data path and Test Data file name
FileOutputStream fileOut = new FileOutputStream(Constant.Path_TestData + Constant.File_TestData);
ExcelWBook.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
and here is my loop
//
ExcelUtils.setExcelFile(Constant.Path_TestData + Constant.File_TestData, "Item_Selection");
List<WebElement> li = driver.findElements(By.cssSelector("#offerVersionSkuListTable>table:nth-child(2)>tbody>tr>td#regularCost"));
for (int j = 0; j<li.size(); j++) {
String cellValue = li.get(j).getText();
List<String> valueList = new ArrayList<String>();
valueList.add(cellValue);
for (int m = 0; m<valueList.size(); m++) {
int temp = row;
ExcelUtils.setCellData(valueList.get(m), temp, Constant.Col_TblRegCost);
temp++;
}
}
Error I am getting is this: org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
After further investigation I found that if I am to remove ExcelUtils.setCellData(valueList.get(m), temp, Constant.Col_TblRegCost);
Loop goes through successfully and prints out values from the table correctly, but I need to write those values to excel. It seems that after writing first value in excel, for loop loops again but does not write anything to excel second time around. Can you help me to find out why?
The explanation to why the for loop only loops once is that the List that you are trying to loop through only contains one value. When the for loop is done looping through that one value, the loop breaks. Same thing as if the List would have five values, the loop would loop through those and then break.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.