我想从第一行读取数据并执行操作,然后写入结果,然后读取第二行,直到Excel文件中没有更多数据。
我的问题:
driver.findElement
做它应该做的事情?主程序
@Test
public void test() throws Exception {
String sPath = "default.xlsx";
DataHelper.setExcelFile(sPath, "sheet1");
XSSFRow row;
XSSFCell cell;
boolean flag = true;
XSSFSheet sheet = ExcelWSheet;
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
row = (XSSFRow) rows.next();
Iterator cells = row.cellIterator();
List <String> obj = new ArrayList<String>();
while (cells.hasNext()) {
cell = (XSSFCell) cells.next();
String cellobj = cell.getStringCellValue();
if(flag)
{
driver.findElement(By.id("text")).sendKeys(cell);
driver.findElement(By.id("text")).click();
driver.findElement(By.id("text")).clear;
}
}
}
}
DataHelper.java
public class DataHelper {
public static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell cell;
private static String path;
public static void setExcelFile(String path, String SheetName) throws Exception {
path = path;
FileInputStream ExcelFile = new FileInputStream(path);
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
}
}
电子邮件,密码
你好,123
hello1,123
答案 0 :(得分:0)
在一般清理(过度使用静态等)之后,迭代器工作得很好。请参阅下面的代码。
关于Selenium电话,我没有足够的知识可以提供帮助。我建议你下次将这个问题作为两个单独的问题发布。我所知道的是,如果要查看文件itselft中的任何更改,则需要将工作簿明确保存到文件。我怀疑这是你想要的。那里有很多易于查找和理解的例子。
public void test() throws Exception {
String sPath = "default.xlsx";
DataHelper dataHelper = new DataHelper(sPath, "sheet1");
XSSFSheet sheet = dataHelper.getExcelWSheet();
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
Row row = rows.next();
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext()) {
Cell cell = cells.next();
String cellobj = cell.getStringCellValue();
if(flag) {
driver.findElement(By.id("text")).sendKeys(cell);
driver.findElement(By.id("text")).click();
driver.findElement(By.id("text")).clear;
}
}
}
}
public class DataHelper {
private XSSFSheet excelWSheet;
private XSSFWorkbook excelWBook;
public DataHelper(String path, String SheetName) throws Exception {
FileInputStream ExcelFile = new FileInputStream(path);
excelWBook = new XSSFWorkbook(ExcelFile);
excelWSheet = excelWBook.getSheet(SheetName);
}
public XSSFSheet getExcelWSheet() {
return excelWSheet;
}
}