HTML Table Sum and Average

I would like to get the sum and the average of the values in a certain column of an HTML table.

Solution

This is possible with a coded solution. The example below is against this W3Schools site.

HtmlTable table = Find.ByTagIndex<HtmlTable>("table", 0); 
 
double r = table.Rows.Count; 
double sum = 0; 
 
for (int i = 0; i < r; i++) 
{ 
    HtmlTableRow row = table.Rows[i]; 
    HtmlTableCell cell = row.Cells[1]; 
 
    string num = cell.TextContent.Substring(0, 2); 
    int j = i + 1; 
    Log.WriteLine("Row" + j.ToString() + ": " + num); 
 
    double percent = Convert.ToDouble(num); 
    sum  = sum + percent; 
} 
 
Log.WriteLine("Sum: " + sum.ToString()); 
double average = sum / r; 
Log.WriteLine("Average: " + average.ToString()); 
Assert.IsTrue(sum == 90); 
Dim table As HtmlTable = Find.ByTagIndex(Of HtmlTable)("table", 0) 
 
Dim r As Double = table.Rows.Count 
Dim sum As Double = 0 
 
For i As Integer = 0 To r - 1 
    Dim row As HtmlTableRow = table.Rows(i) 
    Dim cell As HtmlTableCell = row.Cells(1) 
 
    Dim num As String = cell.TextContent.Substring(0, 2) 
    Dim j As Integer = i + 1 
    Log.WriteLine("Row" + j.ToString() + ": " + num) 
 
    Dim percent As Double = Convert.ToDouble(num) 
    sum = sum + percent 
Next 
 
Log.WriteLine("Sum: " + sum.ToString()) 
Dim average As Double = sum / r 
Log.WriteLine("Average: " + average.ToString()) 
Assert.IsTrue(sum = 90) 
In this article
Not finding the help you need? Improve this article