首页 > 解决方案 > 通过 selenium 使用循环编写 excel 文件

问题描述

我正在尝试将数据从网站写入 excel。该代码正在读取一个输入 Excel 文件并将结果存储在另一个输出文件中。执行我的代码后,我的输出 Excel 文件只包含标题和最后一行数据。由于某种原因,早期的迭代数据没有得到保存。有人可以帮忙吗?

    public class clearenceDetailsTest {
    
    public static void main(String[] args) {  
            
            //  Gecko Driver path   
    System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );  
            
            // Initialize Gecko Driver using Desired Capabilities Class  
      DesiredCapabilities capabilities = DesiredCapabilities.firefox();  
      capabilities.setCapability("marionette",true);  
      WebDriver driver= new FirefoxDriver(capabilities);  
        
        
      // Launch HRMS 
      driver.navigate().to("http://hrms.bankofindia.co.in:8050/psp/HRPROD/EMPLOYEE/HRMS/h/?tab=DEFAULT&cmd=login&errorCode=106&languageCd=ENG");  
      
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      
          // Enter Userid and password  
      driver.findElement(By.id("userid")).sendKeys("20xxxx");  
      driver.findElement(By.id("pwd")).sendKeys("0xxxxxx!");  
        
        
      // Click on login  
    driver.findElement(By.className("psloginbutton")).click();
    
    //  Main Menu
    driver.findElement(By.id("pthdr2wl")).click();
     //  Self Service
    driver.findElement(By.id("pthnavbca_PORTAL_ROOT_OBJECT")).click();
      //View PDC
    driver.findElement(By.id("fldra_CO_EMPLOYEE_SELF_SERVICE")).click();
    driver.findElement(By.className("pthnavscrolldownshow")).click();
    
    
    
        
        
    driver.findElement(By.id("crefli_BOI_VIEW_PDC_CMP_GBL")).click();
    for (int i=1;i<10;i++)  {
    driver.switchTo().frame("ptifrmtgtframe");
    
    
    driver.findElement(By.xpath("//input[starts-with(@Id,'BOI_PEN_EMP_VW_EMPLID')]")).clear();
    
    
    
    
    try {
        
          FileInputStream filein = new FileInputStream(new File("D:\\testdata.xlsx"));      // Location of the Excel sheet
          XSSFWorkbook workbook = new XSSFWorkbook(filein);
          XSSFSheet sheet = workbook.getSheetAt(0);     // Sheet 1
          String pfNumber = sheet.getRow(i).getCell(0).getStringCellValue();
          driver.findElement(By.xpath("//input[starts-with(@Id,'BOI_PEN_EMP_VW_EMPLID')]")).sendKeys(pfNumber); 
          driver.findElement(By.id("#ICSearch")).click();
          String Name=driver.findElement(By.id("PERSONAL_DATA_NAME")).getText();
          String Scale=driver.findElement(By.id("BOI_PEN_EMP_VW_GRADE")).getText();
          String Zone=driver.findElement(By.id("BOI_ZONE_VW_BOI_NAME")).getText();
          String Gender=driver.findElement(By.id("BOI_PEN_EMP_VW_SEX")).getText();  
            
    
          try { 
            
              FileOutputStream out =new FileOutputStream(new File("D:\\testdata - Copy.xlsx")); 
              XSSFWorkbook workbook1 = new XSSFWorkbook();
              XSSFSheet sheet1 = workbook1.createSheet("new sheet");  
              XSSFRow Header = sheet1.createRow(0);
                Header.createCell(0).setCellValue(new XSSFRichTextString("PF Number"));
                Header.createCell(1).setCellValue(new XSSFRichTextString("Name"));
                Header.createCell(2).setCellValue(new XSSFRichTextString("Scale"));
                Header.createCell(3).setCellValue(new XSSFRichTextString("Zone"));
                Header.createCell(4).setCellValue(new XSSFRichTextString("Gender"));  
                  XSSFRow row1 = sheet1.createRow(i);
                  row1.createCell(0).setCellValue(new XSSFRichTextString(pfNumber));
                  row1.createCell(1).setCellValue(new XSSFRichTextString(Name));
                  row1.createCell(2).setCellValue(new XSSFRichTextString(Scale));
                  row1.createCell(3).setCellValue(new XSSFRichTextString(Zone));
                  row1.createCell(4).setCellValue(new XSSFRichTextString(Gender));
                  
                  workbook1.write(out); 
                  out.close();
            
             
             } catch (FileNotFoundException fnfe) {
              fnfe.printStackTrace();
             } catch (IOException ioe) {
              ioe.printStackTrace();
             }
          
          filein.close(); 
         
    } catch (FileNotFoundException fnfe) {
          fnfe.printStackTrace();
         } catch (IOException ioe) {
          ioe.printStackTrace();
         } 
    
    
    // driver.switchTo().parentFrame();
     driver.switchTo().defaultContent();
    driver.findElement(By.id("pthnavbccrefanc_BOI_VIEW_PDC_CMP_GBL")).click();
    
    
    
    }      // for loop
    
        
    
    }    // main
    
        
    }    // class  
  

标签: selenium-webdriver

解决方案


推荐阅读