Raj kumar
Raj kumar

Reputation: 39

How to clear java.lang.IndexOutOfBoundsException in selenium webdriver

Some one help me how to clear (java.lang.IndexOutOfBoundsException) on below code

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("[email protected]")) {
                System.out.println("PASS");
            }
        }

Upvotes: 1

Views: 4057

Answers (2)

Helping Hands
Helping Hands

Reputation: 5396

Modify your loop as per below to remove error :

       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("[email protected]"))
             {
                 System.out.println("PASS"); 
             }
        }

Loop should go till last index so it should be i<=elements.size()-1

Upvotes: 0

Arnaud
Arnaud

Reputation: 17534

Your loop should read :

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

There is no elements.size() index in the List, the last index is elements.size()-1

Upvotes: 1

Related Questions