如何从selenium webdriver中的Excel工作表中读取数据

时间:2014-03-27 13:50:38

标签: java excel selenium logging jxl

我试图从excel文件中读取用户名和密码,下面是我的代码,但它显示以下错误:

  

log4j:WARN找不到记录器的appender   (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)。   log4j:WARN请正确初始化log4j系统。

代码:

public static void main(String[] args) throws BiffException, IOException {      
    Sheet s;
    WebDriver driver = new FirefoxDriver();
    FileInputStream fi = new FileInputStream("E:\\myExcelWorkBook.xls");
    Workbook W = Workbook.getWorkbook(fi);

    s = W.getSheet(0);

    for(int row = 0;row <= s.getRows();row++){

    String Username = s.getCell(0,row).getContents();
    System.out.println("Username" +Username);
    driver.get("AppURL");
    driver.findElement(By.id("txtUserName")).sendKeys(Username);

    String password= s.getCell(1, row).getContents();
    System.out.println("Password "+password);

    driver.findElement(By.id("txtPassword")).sendKeys(password);

    driver.findElement(By.id("btnLogin")).click();
   }
 }

请帮帮我。

5 个答案:

答案 0 :(得分:2)

package com.test.utitlity;

import java.io.IOException;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class readExcel extends globalVariables {

    /**
     * @param args
     * @throws IOException 
     */
    public static void readExcel(int rowcounter) throws IOException{

        XSSFWorkbook srcBook = new XSSFWorkbook("./prop.xlsx");     
        XSSFSheet sourceSheet = srcBook.getSheetAt(0);
        int rownum=rowcounter;
        XSSFRow sourceRow = sourceSheet.getRow(rownum);
        XSSFCell cell1=sourceRow.getCell(0);
        XSSFCell cell2=sourceRow.getCell(1);
        XSSFCell cell3=sourceRow.getCell(2);
        System.out.println(cell1);
        System.out.println(cell2);
        System.out.println(cell3);



}

}

答案 1 :(得分:0)

您的问题是log4j尚未初始化。它不会以任何方式影响您的应用程序的结果,因此忽略或只是初始化Log4J是安全的,请参阅: How to initialize log4j properly?

答案 2 :(得分:0)

不知道你所面临的错误究竟是什么。但是log4j:WARN No appenders could be found for logger error,是由于您已经包含在项目中的log4j jar文件。

需要初始化log4j,但实际上项目不需要Log4j。因此,右键单击您的项目→属性→Java构建路径→库。搜索log4j jar文件并将其删除。

希望它现在能正常运作。

答案 3 :(得分:0)

我使用以下方法来使用excel表格中的输入数据: 还需要导入以下内容

import jxl.Workbook;

然后

Workbook wBook = Workbook.getWorkbook(new File("E:\\Testdata\\ShellData.xls"));
//get sheet
jxl.Sheet Sheet = wBook.getSheet(0); 
//Now in application i have given my Username and Password input in following way
driver.findElement(By.xpath("//input[@id='UserName']")).sendKeys(Sheet.getCell(0, i).getContents());
driver.findElement(By.xpath("//input[@id='Password']")).sendKeys(Sheet.getCell(1, i).getContents());
driver.findElement(By.xpath("//input[@name='Login']")).click();

它会起作用

答案 4 :(得分:-1)

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

String FilePath = "/home/lahiru/Desktop/Sample.xls";
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);

String <variable> = sh.getCell("A2").getContents();