Java 如何在 selenium webdriver 中从 Excel 工作表中读取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22689666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read Data from Excel sheet in selenium webdriver
提问by Khushali
I'm trying to read username and password from the excel file, below is my code but it shows following error :
我正在尝试从 excel 文件中读取用户名和密码,下面是我的代码,但显示以下错误:
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager). log4j:WARN Please initialize the log4j system properly.
log4j:WARN 找不到记录器 (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) 的附加程序。log4j:WARN 请正确初始化 log4j 系统。
Code:
代码:
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();
}
}
Please help me out.
请帮帮我。
回答by JpR
Your problem is that log4j has not been initialized. It does not affect the outcome of you application in any way, so it's safe to ignore or just initialize Log4J, see: How to initialize log4j properly?
您的问题是 log4j 尚未初始化。它不会以任何方式影响您的应用程序的结果,因此忽略或仅初始化 Log4J 是安全的,请参阅: 如何正确初始化 log4j?
回答by user2416212
Don't know about what the error you are facing exactly. But log4j:WARN No appenders could be found for logger error
, is due to the log4j jar file that you have included in your project.
不知道您所面临的错误究竟是什么。但是log4j:WARN No appenders could be found for logger error
,这是由于您的项目中包含了 log4j jar 文件。
Initializing log4j is needed but actually Log4j is not necessary for your project. So Right click on your Project → Properties → Java Build Path → Libraries.. Search for log4j jar file and remove it.
需要初始化 log4j,但实际上 Log4j 对于您的项目不是必需的。因此,右键单击您的项目 → 属性 → Java 构建路径 → 库。搜索 log4j jar 文件并将其删除。
Hope it will work fine now.
希望它现在能正常工作。
回答by Amit
i have used following method to use input data from excel sheet: Need to import following as well
我使用以下方法来使用 Excel 表中的输入数据: 还需要导入以下内容
import jxl.Workbook;
import jxl.Workbook;
then
然后
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();
it will Work
它会工作
回答by ChanGan
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);
}
}
回答by lahiru vikasitha
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();