如何在selenium webdriver中清除java.lang.IndexOutOfBoundsException

时间:2015-12-11 11:04:39

标签: java selenium-webdriver

有人帮我如何清除(java.lang.IndexOutOfBoundsException)下面的代码

List<WebElement> elements = dr.findElements(By
                .xpath("//span[@class='small-info']"));
        System.out.println("NUMBER OF ROWS IN THIS TABLE = " + elements.size());
        for (int i = 0; i <= elements.size(); i++) {
            WebElement ele = elements.get(i);
            System.out.println(ele.getText());
            if (ele.getText().contains("chri@en.com")) {
                System.out.println("PASS");
            }
        }

2 个答案:

答案 0 :(得分:1)

你的循环应该是:

for (int i = 0; i<elements.size(); i++)

列表中没有elements.size()索引,最后一个索引是elements.size()-1

答案 1 :(得分:0)

按照以下修改循环以删除错误:

       for (int i = 0; i<=elements.size()-1; i++){
            String str = elements.get(i).getText();
             //WebElement ele = elements.get(i);
             //System.out.println(ele.getText());
            System.out.println(str);
             if (str.contains("chri@en.com"))
             {
                 System.out.println("PASS"); 
             }
        }

循环应该到最后一个索引,所以它应该是i<=elements.size()-1