在selenium web驱动程序中从excel读取数据时程序终止

时间:2014-12-03 12:11:27

标签: excel selenium-webdriver

我将控制台输出值与excel数据匹配。并且在excel中尝试在发现匹配时设置“TRUE”或“FALSE”。我的计划如下:

    WebDriver driver = new FirefoxDriver();

    driver.get("http://en.wikipedia.org/wiki/Software_testing");
    String title = driver.getTitle();
    System.out.println(title);


    FileInputStream input = new FileInputStream("D:\\book.xls");

    FileOutputStream webdata = new FileOutputStream ("D:\\book.xls");


    int count=0;

    HSSFWorkbook wb = new HSSFWorkbook(input);
    HSSFSheet sh = wb.getSheet("sheet1");
    HSSFRow row = sh.getRow(count);
    String data = row.getCell(1).toString();
    System.out.println(data);



    if(title==data)

    {

        row.createCell(10).setCellValue("TRUE");
        wb.write(webdata);

    }
    else
    {

        row.createCell(10).setCellValue("FALSE");
        wb.write(webdata);

    }


    wb.close();
    input.close();
    driver.close(); 

当程序到达此处时:HSSFWorkbook wb = new HSSFWorkbook(输入);它终止了。我已经调试但没有得到解决方案。有人可以帮忙吗?

我在控制台中遇到以下错误:

 Exception in thread "main" java.io.IOException: Unable to read entire header; 0 bytes read;       expected 512 bytes
at org.apache.poi.poifs.storage.HeaderBlock.alertShortRead(HeaderBlock.java:227)
at org.apache.poi.poifs.storage.HeaderBlock.readFirst512(HeaderBlock.java:208)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:128)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:361)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:342)
at mhover.main(mhover.java:52)

1 个答案:

答案 0 :(得分:1)

现在解决它。问题就像:

FileInputStream input = new FileInputStream("D:\\book.xls");
FileOutputStream webdata = new FileOutputStream ("D:\\book.xls");

上面的代码覆盖了我的excel文件数据,并且使excel空白。所以我移动了代码: FileOutputStream webdata = new FileOutputStream(&#34; D:\ book.xls&#34;); 一旦数据读取过程完成。所以它现在正在工作。

新&amp;工作代码:

     //CODE TO REMOVE UNNECESSARY WARNING
    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");


    //CALL FIREFOX DRIVER TO OPEN IT
    WebDriver driver = new FirefoxDriver();


    driver.get("http://en.wikipedia.org/wiki/Software_testing");
    String title = driver.getTitle();
    System.out.println(title);



    FileInputStream input = new FileInputStream("D:\\sel.xls");

    int count=0;

    HSSFWorkbook wb = new HSSFWorkbook(input);
    HSSFSheet sh = wb.getSheet("sheet1");
    HSSFRow row = sh.getRow(count);
    String data = row.getCell(1).toString();
    System.out.println(data);


    FileOutputStream webdata = new FileOutputStream ("D:\\sel.xls");

    if(title.equals(data))

    {

        row.createCell(10).setCellValue("TRUE");
        wb.write(webdata);

    }
    else
    {

        row.createCell(11).setCellValue("FALSE");
        wb.write(webdata);

    }

    driver.close(); 
    wb.close();
    input.close();


}


}