Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
897 views
by Contributing Tester (14 points)
I desing a script to print all cell text  Below is code for same .It show me error

java.lang.IndexOutOfBoundsException: Index: 5, Size: 5

How Can i avoid this exception error ?

public class WebTableClass
{
    WebDriver driver;
    String path = System.getProperty("user.dir");
    public void openbrower()
    {
        System.setProperty("webdriver.chrome.driver",path+ "//Browser//chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("http://only-testing-blog.blogspot.in/2014/05/form.html");
    }
    
    public void webtable()
    {
        /*First Locate table on which we have to perform action
        tbody =table body*/
        WebElement table = driver.findElement(By.xpath(("(//tbody)[position()=1]")));
        
        /*Now get all rows of selected table
         * tr =table row*/
        
        List<WebElement> table_row =table.findElements(By.tagName("tr"));
        
        /*Now get all rows count of table*/
        
        int table_row_count = table_row.size();
        
        /*this loop execute till all row count*/
        try {
        
        for (int row =1; row<table_row.size(); row++)
        {
            /*now get table cell(table column) of table*/
            
            List<WebElement> table_cell =table.findElements(By.tagName("td"));
            int table_cell_count =table_cell.size();
            System.out.println("table row " +table_row_count + "table column" +table_cell_count);
            
              for (int cell =1;cell <table_cell.size(); cell++)
               {
                    /*not get text from particular column cell*/
                    
                  String table_cell_text =table_row.get(cell).getText();
                  System.out.println("Cell Value Of row number "+row+" and column number "+cell+" Is " +table_cell_text);
                    
               }
        }
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
    
    public static void main (String args[])
    {
        WebTableClass table_object =new WebTableClass();
        table_object.openbrower();
        table_object.webtable();
    }
}
by
Which point are you getting an exception

1 Answer

1 like 0 dislike
by
selected by
 
Best answer

There is a problem with your code here,

 

In table_cell you are getting it as 

List<WebElement> table_cell =table.findElements(By.tagName("td"));

you're supposed to be getting cells from the row.

List<WebElement> table_cell =table_row.get(row ).findElements(By.tagName("td"));

Hope that helps!

 

 

by Contributing Tester (14 points)
Thanks...Its works fine now


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...