java - for循环中的数组越界错误(在Selenium Webdriver,Java中读取excel文件)

标签 java selenium

这是全部代码。

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/

相关文章:

java - 这个 For 循环有什么问题?

java - 通用接口(interface)与每种类型的接口(interface)

java - 通过正则表达式切割文本

Python 脚本未运行且未给出错误

php - 无法调用具有多个方法参数的命令

java - Android 在开始 Activity 时崩溃?

java - InputSource 和 InputStream 有什么区别?

javascript - Protractor - 如何通过自定义(非 HTML)属性定位元素?

java - 如何在@AfterMethod 中获取TestNG/Selenium 的测试结果状态?

java - Selenium webdriver :org. openqa.selenium.InvalidElementStateException:元素已禁用,因此可能无法用于操作