[selenium-users] java.lang.IndexOutOfBoundsException: While finding total links

162 views
Skip to first unread message

phaneendra

unread,
Sep 18, 2016, 6:12:21 AM9/18/16
to seleniu...@googlegroups.com
Hello Team,

Getting an error when executed below Script.

package linksCount;

import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SLinksCount {
  @Test
  public void FindAllLinks() {
   
 System.out.println("launching chrome browser");
 System.setProperty("webdriver.chrome.driver", "H:\\chromedriver.exe");
 
 WebDriver driver = new ChromeDriver();
 driver.manage().window().maximize();
 
 java.util.List<WebElement> li = driver.findElements(By.tagName("a"));
 System.out.println(li.size());
 
 for (int i = 1; i<=li.size(); i=i+1) 
 {
 System.out.println(li.get(i).getText());
 }
 driver.close();
  }
}


Please find log file  for an error.


--
Cheers,
Phaneendra


log.txt

Praveen Kumar

unread,
Sep 18, 2016, 6:29:31 AM9/18/16
to seleniu...@googlegroups.com, phanee...@gmail.com
use i<li.size() instead of i<=li.size()

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/CAAoFcgOFaUa9hC%2BuP_25RST%3DiUcfMT0NimwGu6%3D8qHL_eftTEQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Aniket Mane

unread,
Sep 18, 2016, 6:37:16 AM9/18/16
to seleniu...@googlegroups.com
Yes Exactly praveen is right...You are using wrong condition that's why it is throwing indexoutofbound exception.
li.size will give total links in list 1 to 47 and link list starts from 0th index hence i<=li.size will definitely fail.

Thanks,
Aniket

To post to this group, send email to seleniu...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

phaneendra

unread,
Sep 18, 2016, 6:50:40 AM9/18/16
to seleniu...@googlegroups.com
Thanks Praveen and Aniket.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Cheers,
Phaneendra


Reply all
Reply to author
Forward
0 new messages