我的情况是我有一个三行两列的 Excel。第一列是用户名,第二列是密码,下面三行包含一些数据。
我的目标是我的 webdriver 脚本读取第一行数据并尝试登录,然后读取第二行数据并再次尝试登录,依此类推。 我创建的脚本是读取第一个用户名并将其放入用户名文本框中,读取第一个密码并输入密码,然后尝试登录,并且永远不会进入第二行数据。我的猜测是我在脚本中使用的中断位置不正确 我不知道应该在哪里打破从一行到另一行的循环。
这是我的代码:
FileInputStream loginFile = new FileInputStream(new File("C:\\Selenium\\Input_Data\\LoginFailure.xlsx"));
//Get the workbook instance for xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(loginFile);
//Get the first sheet of the xlsx file
XSSFSheet sheet = workbook.getSheet("Login");
//Iterate through each row from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext())
{
Row row = rowIterator.next();
//For each row iterate through each columns
Iterator <Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext())
{
Cell cell = cellIterator.next();
if(cell.getColumnIndex()==0)
{
driver.findElement (By.id("UserName")).sendKeys(cell.getStringCellValue());
}
else
driver.findElement(By.id("Password")).sendKeys(cell.getStringCellValue());
}
break;
}
driver.findElement(By.id("LoginButton")).click();
System.out.println("**********User login failure**********");
最佳答案
不要放break..请尝试放继续并让我知道结果。
break 会跳出循环,而 continue 会进入下一次迭代。
关于java - 如何使用 Selenium 和 POI 从 Excel 的第一行迭代并尝试登录,然后再次迭代到第二行并尝试再次登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31755486/