Warning: fopen(/tmp/SghXTngBZPli-MoTXxc.tmp): failed to open stream: Disk quota exceeded in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 139

Warning: unlink(/tmp/SghXTngBZPli-MoTXxc.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
Site Loader
Auckland, New Zealand
In this post we will discuss working with tables using Selenium. Basically, all the tables rendered in web pages are html tables (other than some third party controls such as telerik or infragistic controls tables) In automation of application, we will encounter places to
  • Read the table
  • Get a cell value
  • Compare the table value with database
Hence, its very important for anyone to test tables.

Simple table

Simple table we are going to work with for this demonstration will look something like this

Simple algorithm to read the table

Here is the complete video of the above discussion Here is the complete code from the above video

Read Table

        static List _tableDatacollections = new List();

        public static void ReadTable(IWebElement table)
        {
            //Get all the columns from the table
            var columns = table.FindElements(By.TagName("th"));
            //Get all the rows
            var rows = table.FindElements(By.TagName("tr"));
            //Create row index
            int rowIndex = 0;
            foreach (var row in rows)
            {
                int colIndex = 0;
                var colDatas = row.FindElements(By.TagName("td"));
                foreach (var colValue in colDatas)
                {
                    _tableDatacollections.Add(new TableDatacollection
                    {
                        RowNumber = rowIndex,
                        ColumnName = columns[colIndex].Text != "" ?
                                     columns[colIndex].Text : colIndex.ToString(),
                        ColumnValue = colValue.Text,
                    });
                    //Move to next column
                    colIndex++;
                }
                rowIndex++;
            }
        }

Read Cell

        public static string ReadCell(string columnName, int rowNumber)
        {
            var data = (from e in _tableDatacollections
                        where e.ColumnName == columnName && e.RowNumber == rowNumber
                        select e.ColumnValue).SingleOrDefault();

            return data;
        }
Thanks for reading the post and watching the video !!! Please leave your comments and let me know if there is anything I should update in this post. Thanks, Karthik KK

Post Author: Karthik kk

4 Replies to “Working with simple tables using Selenium”

  1. foreach (var colValue in colDatas)
    {
    _tableDatacollections.Add(new TableDatacollection
    {
    RowNumber = rowIndex,
    ColumnName = columns[colIndex].Text != “” ?
    columns[colIndex].Text : colIndex.ToString(),
    ColumnValue = colValue.Text,

    });

    I get an exception here

  2. System.ArgumentOutOfRangeException occurred
    HResult=-2146233086
    Message=Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    ParamName=index
    Source=mscorlib
    StackTrace:
    at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
    InnerException:

  3. Hello,

    Is there a solution on the exception: “System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index”?

    Looking at the example below, when there are no table/column header on the operation columns (e.g. edit, show, destroy links), I believe this happens when you collect table data and store columns = and count of is less than number of columns in . The below excample, = 5 less than = 7.
    Thus exception is returned.

    Would be very nice to have the solution on this 🙂 thanks in advance.

    First name
    Last name
    City
    State

    Jonas
    Dibley
    City Test 02
    AZ
    Show
    Edit
    Destroy

    Danny
    Glover
    Simino
    IA
    Show
    Edit
    Destroy

Leave a Reply

Your email address will not be published. Required fields are marked *