[英]Fetching value from webtable and write in excel sheet one by one of each row in a loop
1) I am trying to read the values from the webtable of an appliation and write it into the excel sheet one by one. 1)我试图从应用程序的网络表中读取值并将其一一写入excel表中。
2) There are 4 values in each row of the webtable that need to be written to excel sheet, but there are some images in each row that i'm ignoring bu using below code. 2) webtable 的每一行有 4 个值需要写入 Excel 工作表,但每行中有一些图像我使用下面的代码忽略了 bu。
text.length()>2
3) There will be 200-300 rows in webtable that need to be fetched and written to excel sheet. 3)webtable 中会有 200-300 行需要提取并写入 Excel 表格。
This is the code i have tried.这是我试过的代码。 But i dont know how to write it into each row of the excel sheet one by one.但我不知道如何将其一一写入excel表的每一行。 Please help me in this regard.请在这方面帮助我。
//get the table WebElement statusTable = browser.findElement(By.id("projectstatus")); //Get all the rows in the table List<WebElement> allRows = statusTable.findElements(By.tagName("tr")); //Get the size(row no) of allRows int rowSize = allRows.size(); System.out.println(rowSize); for (WebElement row : allRows) { //Get all cell values in each row List<WebElement> allCells = row.findElements(By.tagName("td")); //System.out.println(allCells.size()); if(allCells.size() > 1){ for (WebElement cell : allCells) { String text = cell.getText(); if(text.length()>2){ String value = cell.getText(); } } } // locate the test xl file File file = new File("e:\\\\Testing_emi.xls"); // create input stream FileInputStream fis = new FileInputStream(file); // create workbook HSSFWorkbook wb = new HSSFWorkbook(fis); // get sheet HSSFSheet sheet1 = wb.getSheet("Sheet1"); // get rows HSSFRow row = sheet1.getRow(1); HSSFCell cellEx = row.getCell(0); if (cellEx == null) { cellEx = row.createCell(0); } cellEx.setCellValue(value);
//get the table
WebElement statusTable = browser.findElement(By.id("projectstatus"));
//Get all the rows in the table
List<WebElement> allRows = statusTable.findElements(By.tagName("tr"));
//Get the size(row no) of allRows
int rowSize = allRows.size();
System.out.println(rowSize);
// locate the test xls file
File file = new File("e:\\Testing_emi.xls");
// create input stream
FileInputStream fis = new FileInputStream(file);
// create workbook
HSSFWorkbook wb = new HSSFWorkbook(fis);
// get sheet
HSSFSheet sheet1 = wb.getSheet("Sheet1");
// get rows
HSSFRow row;
for (int i=0; i<rowSize; i++)
{
WebElement webRow = allRows.get(i);
//Get all cell values in each row
List<WebElement> allCells = webRow.findElements(By.tagName("td"));
//System.out.println(allCells.size());
if(allCells.size() > 1)
{
HSSFRow excelRow = sheet1.createRow(i);
for (int j=0; j<allCells.size(); j++)
{
WebElement webCell = allCells.get(j);
String text = webCell.getText();
if(text.length()>2)
{
Cell excelCell = excelRow.createCell();
excelCell.setValue(webCell.getText());
}
}
}
}
sheet1.close();
Read the values from the Web Table of an application and write it in to the excel sheet one by one.There are 5 values in each row of the Web Table and that need to written in Excel sheet.从应用程序的Web Table 中读取值并逐个写入Excel 表格中。Web Table 每行有5 个值需要写入Excel 表格。 Below is the working code.下面是工作代码。
System.setProperty("webdriver.chrome.driver","C:\\Users\\Documents\\Selenium jars\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://cosmocode.io/automation-practice-webtable/");
WebElement table = driver.findElement(By.xpath("//*[@id='countries']/tbody"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
int rowcount = rows.size();
FileOutputStream fis = new FileOutputStream(new File("C:\\Users\\Documents\\Selenium\\output.xlsx"));
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sh = wb.createSheet("First Sheet");
for (int row = 0; row < rowcount; row++) {
List<WebElement> columns_inrow = rows.get(row).findElements(By.tagName("td"));
int columns_count = columns_inrow.size();
System.out.println("Number of cells in Row " + row + " are " + columns_count);
Row ro = sh.createRow(row);
for (int column = 0; column < columns_count; column++) {
String celltext = columns_inrow.get(column).getText();
System.out.println(
"Cell Value of row number " + row + " and column number " + column + " Is " + celltext);
ro.createCell(column).setCellValue(celltext);
}
System.out.println("===========================");
}
wb.write(fis);
wb.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.