从webtable中获取值并在循环中逐行写入excel表中的每一行

时间:2015-05-14 11:01:31

标签: java excel selenium selenium-webdriver

1)我正在尝试从应用程序的webtable中读取值,并将其逐个写入excel表。

2)webtable的每一行中有4个值需要写入excel表,但每行中都有一些图像,我使用下面的代码忽略了bu。



text.length()>2



 。

3)webtable中将有200-300行需要获取并写入excel表。

这是我试过的代码。但我不知道如何将它逐一写入excel表的每一行。请帮助我。



        //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);
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

//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();

答案 1 :(得分:0)

从应用程序的Web表中读取值,并将其逐个写入excel工作表。WebTable的每一行中有5个值,需要在Excel工作表中编写。下面是工作代码。

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();

    }