Warning: fopen(/tmp/SghXTngBZPli-EAnnzm.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-EAnnzm.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 the last post we discussed how to work with simple table using Selenium, in this post we will discuss working with complex table of html using selenium

Complex Table

The table looks something like this

Things to note in the table

This table is complex because as you can see there is
  • A column with NO header
  • Two button with no unique identification properties
  • Check box in option column with no id or name, also there is no text J

Coding Complexity

We have used little advanced coding concepts of C# in this post, since this makes lines of codes reduced a lot as well. In this post we have used
  1. LINQ
  2. Yield return
  3. Collections with custom classes
  4. Null interpolation
But, the code is clearly explained in the video below Here is the complete video of the above discuss Here is the complete code from the above video

Read Table

       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,
                        ColumnSpecialValues = colValue.Text != "" ? null :
                                              colValue.FindElements(By.TagName("input"))
                    });

                    //Move to next column
                    colIndex++;
                }
                rowIndex++;
            }
        }

PerformActionOnCell

       public static void PerformActionOnCell(string columnIndex, string refColumnName, string refColumnValue,
            string controlToOperate = null)
        {
            foreach (int rowNumber in GetDynamicRowNumber(refColumnName, refColumnValue))
            {
                var cell = (from e in _tableDatacollections
                            where e.ColumnName == columnIndex && e.RowNumber == rowNumber
                            select e.ColumnSpecialValues).SingleOrDefault();


                //Need to operate on those controls
                if (controlToOperate != null && cell != null)
                {
                    var returnedControl = (from c in cell
                                           where c.GetAttribute("value") == controlToOperate
                                           select c).SingleOrDefault();
                    //ToDo: Currenly only click is supported, future is not taken care here
                    returnedControl?.Click();
                }
                else
                {
                    cell?.First().Click();
                }
            }
        }



        private static IEnumerable GetDynamicRowNumber(string columnName, string columnValue)
        {

            //dynamic row
            foreach (var table in _tableDatacollections)
            {
                if (table.ColumnName == columnName && table.ColumnValue == columnValue)
                    yield return table.RowNumber;
            }

        }
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

2 Replies to “Working with complex table using Selenium”

  1. Hey Karthik,
    Firstly Great work on the tutorial!!

    I have a slightly different scenario than the one you have explained in the video. It goes like
    I have a table with a column without a name in which each cell consists of a checkbox. I have another column named “Format” which has a single image depending on the type of format(I have multiple images for different formats). Now my question is how can I check the checkbox(multiple rows) based on the image that is available in the “Format” column.
    Note that I am dealing with two different ‘ColumnSpecialValues’ which are “img” and “input”. The only way I can distinguish between the format images are its src tag. I am not sure how I can pass this value to your ‘GetDynamicRowNumber’ method.

    Hope I was clear on describing the scenario. Looking forward for you suggestions.

    1. In GetDynamicRowNumber method, there is only one ColumnSpecialValue, you need to extend the method depending upon your need.

      In your case, you need to create one more ColumnSpecialValue or may be List and based on that you can select the checkbox from all the columns you have the img. You need to use the columnoffset (index) to get the column which you need to deal with.

      Hope this shed some lights on what you are looking for.

      Thanks,
      Karthik KK

Leave a Reply

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