这是全部代码。
for (int i = 0; i <= 1; i++, column++, row++) {
// read cell, input coordinates: (column, row)
Cell c1 = sh1.getCell(column, row);
Cell c2 = sh1.getCell(column + 1, row);
Cell c3 = sh1.getCell(column + 2, row);
Cell c4 = sh1.getCell(column + 3, row);
// return string content
String locationCode = c1.getContents();
String locationName = c2.getContents();
String description = c3.getContents();
String address = c4.getContents();
driver.findElement(By.id("tab7")).click();
driver.findElement(By.id("subtab2")).click();
driver.findElement(By.id("new")).click();
driver.findElement(By.id("ExtLocationCode")).clear();
driver.findElement(By.id("ExtLocationCode")).sendKeys(locationCode);
driver.findElement(By.id("LocationName")).clear();
driver.findElement(By.id("LocationName")).sendKeys(locationName);
driver.findElement(By.id("Description")).clear();
driver.findElement(By.id("Description")).sendKeys(description);
driver.findElement(By.id("address1")).clear();
driver.findElement(By.id("address1")).sendKeys(address);
driver.findElement(By.id("save")).click();
driver.findElement(By.id("actions")).click();
driver.findElement(By.id("save")).click();
}
运行后,它说:Exception in thread "main"java.lang.ArrayIndexOutOfBoundsException: 4
它不会继续处理 excel 文件中单元格的下一列和行。我的代码有什么问题?
最佳答案
您正在递增 for 循环定义中的列,然后:
Cell c1 = sh1.getCell(column,row);
Cell c2 = sh1.getCell(column+1,row);
Cell c3 = sh1.getCell(column+2,row);
Cell c4 = sh1.getCell(column+3,row);
我认为您不需要在 for 循环定义中递增列 for(int i = 0; i<=1; i++, column++, row++)
除非你有一个非常奇怪的 excel 工作表布局。尝试 for(int i = 0; i<=1; i++, row++)
关于java - for循环中的数组越界错误(在Selenium Webdriver,Java中读取excel文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262685/