Error message java.lang.IndexOutOfBoundsException is displayed when working with RadioButtons session that was posted in www.Toolsqa.com

There was a session in www.toolsqa.com where Radio buttons is explained and I copied the code snippet on to my eclipse but got an error message  java.lang.IndexOutOfBoundsException. When I tried to output the Size of the radio buttons it displayed me 2. But there is some problem with the for loop and unable to get the gettext() of the 2 radio buttons. I was expecting to print out the 2 radio buttons
Male
Female

Is there any problem with the For loop?


WebDriver driver = new FirefoxDriver();
// Put an Implicit wait,

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Launch the URL

driver.get("http://www.toolsqa.com/automation-practice-form");
List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex"));
System.out.println(rdBtn_Sex.size());
for (int i = 1; i<=rdBtn_Sex.size(); i=i+1)
{

System.out.println(rdBtn_Sex.get(i).getText());
}
Train Asked on October 13, 2015 in Selenium WebDriver.
Add Comment
  • 3 Answer(s)

    for (int i = 1; i<=rdBtn_Sex.size(); i=i+1)

    should be

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

    Professor Answered on October 13, 2015.
    Add Comment

    No I tried the For loop as you said but this time it didn’t throw any IndexOutOfBounds exception but didn’t display anything on the console. My updated code is as below

    Is there any error with using get(i)?

    
    public static void main(String[] args) {
    WebDriver driver = new FirefoxDriver();
    // Put an Implicit wait,
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    // Launch the URL
    
    driver.get("http://www.toolsqa.com/automation-practice-form");
    List<WebElement> rdBtn_Sex = driver.findElements(By.name("sex"));
    int j=rdBtn_Sex.size();
    
    for (int i = 0; i<j; i++)
    {
    
    System.out.println(rdBtn_Sex.get(i).getText());
    }
    driver.quit();
    
    Train Answered on October 13, 2015.
    Add Comment

    Yes I got it I tried to work in a way to print out like as below using getattribute(“value”). It worked thanks

    System.out.println(rdBtn_Sex.get(i).getAttribute(“value”))

    Train Answered on October 13, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.