Java selenium 获取表格数据_Selenium Webdriver如何通过获取Excel输入从表中选择记录...

据我所知,您正在尝试从远程位置读取文件,然后从中读取信息 . 如果您可以使用Apache POI库在运行时读取内容,那将是一个很好的做法 .

在我的项目中,我使用Apache POI库读取Excel工作表中的所有内容,以设置变量的值 . 这是我如何实现它的代码片段 . 希望这将指导您找到合适的解决方案 . :)

public void readExcelDoc() throws FileNotFoundException, IOException

{

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("excelDoc//scripts.xls"));

HSSFWorkbook wb = new HSSFWorkbook(fs);

HSSFSheet sheet = wb.getSheetAt(0);

HSSFRow row = null;

HSSFCell cell = null;

int rows = 0; // No of rows

// rows = sheet.getPhysicalNumberOfRows();

rows = sheet.getLastRowNum();

int cols = 2; // No of columns

int tmp = 0;

// This trick ensures that we get the data properly even if it doesn't start from first few rows

for(int i = 0; i < 10 || i < rows; i++) {

row = sheet.getRow(i);

if(row != null) {

tmp = sheet.getRow(i).getPhysicalNumberOfCells();

if(tmp > cols) cols = tmp;

}

}

int testRowNo = 0;

String rowName = "Test Name";

String columnValue = " ";

//Iterate through Row and columns here. Excluding 1st row for title names

for(int r = 1; r <= rows; r++) {

row = sheet.getRow(r);

if(row != null) {

//Browse through columns using c

for(int c = 0; c < cols; c++) {

if(c==0) //Only taking data from Cell 0; Ignoring any other inputs

{

cell = row.getCell((short)c);

try

{

if(cell.getStringCellValue().contains(rowName))

{

testRowNo =row.getRowNum();

}

if(testRowNo > 0 )

{

if(cell.getColumnIndex() == 0 && row.getRowNum() > testRowNo && cell.getStringCellValue().length() !=0)

{

try{

String cellValue = cell.getStringCellValue().toLowerCase();

//System.out.println(cellValue);

scriptType.add(cellValue);

}

catch(IllegalStateException e)

{

e.printStackTrace();

scriptType.add(cell.getStringCellValue());

}

}

}

}

catch(NullPointerException e)

{

}

}

if(c==1)

{

cell = row.getCell((short)c); //this sets the column number

if(testRowNo == 0)

{

try{

String cellValue = cell.getStringCellValue();

//System.out.println(cellValue);

columnValue = cellValue;

}

catch(IllegalStateException e)

{

String cellValue = cell.toString();

columnValue = cellValue;

}

catch(NullPointerException e)

{

String cellValue = nodata;

columnValue = cellValue;

}

}

}

if(c==2)

{

cell = row.getCell((short)c); //this sets the column number

if(testRowNo == 0)

{

try{

String cellValue = cell.getStringCellValue();

//System.out.println(cellValue);

inputParameters.put(cellValue, columnValue);

}

catch(IllegalStateException e)

{

String cellValue = cell.toString();

inputParameters.put(cellValue, columnValue);

}

catch(NullPointerException e)

{

String cellValue = nodata;

inputParameters.put(cellValue, columnValue);

}

}

}

}

}

}

System.out.println("---------The parameters set from excel are : ---------");

@SuppressWarnings("rawtypes")

Iterator iterator = inputParameters.keySet().iterator();

while (iterator.hasNext()) {

String key = iterator.next().toString();

String value = inputParameters.get(key).toString();

System.out.println(key + " : " + value);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值