获取“ java.lang.IndexOutOfBoundsException:索引:2,大小:2”异常

时间:2019-05-25 05:01:39

标签: java

运行硒代码时出现索引2大小为2的异常。我正在尝试类似的情况

单击链接,然后获取元素列表和getText 从Gettext,我正在获取数值 使用我正在循环并执行操作的每个元素的数值 在这里,第一个元素的大小是2,循环执行2次,下一个元素的数目是4,但是循环执行3次,并获得异常

public static  void elements(String xpath,String lixpath,String selectXpath) throws InterruptedException {
    WebElement ErrorType=driver.findElement(By.xpath(xpath));
    java.util.List<WebElement> links=ErrorType.findElements(By.xpath(lixpath));
    int length=links.size();

    System.out.println("length of the blocks:"+length);

    for(int i=0;i<length;i++) {
        System.out.println("%%%%%%%%%%%%%%%%%");
        System.out.println("loop1 for 1st ");
        String text=links.get(i).getText();
        System.out.println("text value:"+text);
        String textconversion=text.substring(text.indexOf("(")+1,text.indexOf(")"));
        int unitvalue=Integer.parseInt(textconversion);
        System.out.println("units value: "+unitvalue);//2           
        for( int j=1;j<=unitvalue;j++) {
            System.out.println("********");
            System.out.println(length+"value of j:"  +j);
            links.get(j).click();
            Thread.sleep(1000);
            Select select = new Select(driver.findElement(By.xpath(selectXpath)));
            Thread.sleep(2000);
            select.selectByIndex(3);
            Thread.sleep(3000);

           }
        }

     }

输出为:

length of the blocks:2
%%%%%%%%%%%%%%%%%
loop1 for 1st 
text value:HL/HL BEER (2)
units value: 2
********
2value of j:1
********
2value of j:2

0 个答案:

没有答案