HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Retrieving value from HTML table

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
valueretrievingfromhtmltable

Problem

Currently all this does is retrieve data from specific columns, the final result will be that I will check the data to make sure that it passes the test cases, but I want to look at this foreach right now and see if I can't make it a little simpler.

A lot of what I have coded I have come back and turned into Linq statements and made things a lot simpler (shorter), and I would really like to do the same with this foreach statement, but not real sure how to do it.

I hate to admit that I am very new to using Linq, I can read it for the most part but still have a little difficulty when writing more complex statements/queries.

Is there something that I can do to make this a lot more simple before I code any more on this Scenario, I would like to simplify so that I don't write extraneous code that will be refactored out when I make this more clear.

I have also been trying to find something that imports the HTML table into a nice neat object that I can query, but everything I have found so far has been similar to what I am doing or more complex than what I am doing here.

```
[Then(@"see the '(.)' of '(.)'")]
public void ThenSeeThe(string columnHeader, string entryName)
{
var htmlTable = Ie.FindElementByClassName("table");
var tableHeaders = htmlTable.FindElements(By.TagName("th"));
var rows = new List>();

var htmlRows = htmlTable.FindElements(By.TagName("tr"));

foreach (var htmlRow in htmlRows)
{
//first row holds the table headers
if (htmlRow.FindElements(By.TagName("th")).Count > 0)
{
var thList = tableHeaders.Select(th => th.Text.ToString()).ToList();
rows.Add(thList);
continue;
}

var cellsInRow = htmlRow.FindElements(By.TagName("td"));
var cells = cellsInRow.Select(cell => cell.Text.ToString()).ToList();
rows.Add(cells);
}

var columnIndex = rows.First().IndexOf(columnHeader);
var field = rows.Where(x => x[0] == entryName).Select(x

Solution

If I understand correctly, the role of this procedure (which is far from obvious from the name alone) is to assign either _CreateUTC or _ModifyUTC values (which should be _createUTC and _modifyUTC if they're private fields - always use camelCase for fields and locals/parameters.

You don't need that rows list of lists of strings at all, let alone iterate that monster afterward to retrieve a value.

Just loop and locate the value as you're looping, don't store anything but what you need and stop looping once you got it - I think something like this would do it:

var headerIndex = 0;
DateTime value;
foreach (var htmlRow in htmlRows)
{
    if (htmlRow.FindElements(By.TagName("th")).Count > 0)
    {
        headerIndex = tableHeaders.Select(th => th.Text.ToString()).IndexOf(columnHeader);
        continue;
    }

    var cellsInRow = htmlRow.FindElements(By.TagName("td")).ToArray();
    if (cellsInRow[0] == entryName)
    {
        value = Convert.ToDateTime(cellsInRow[headerIndex].Text.ToString());
        break;
    }
}

if (columnHeader = "Create UTC Date")
{
    _createUTC = value;
}
else if (columnHeader = "Modify UTC Date")
{
    _modifyUTC = value;
}

Code Snippets

var headerIndex = 0;
DateTime value;
foreach (var htmlRow in htmlRows)
{
    if (htmlRow.FindElements(By.TagName("th")).Count > 0)
    {
        headerIndex = tableHeaders.Select(th => th.Text.ToString()).IndexOf(columnHeader);
        continue;
    }

    var cellsInRow = htmlRow.FindElements(By.TagName("td")).ToArray();
    if (cellsInRow[0] == entryName)
    {
        value = Convert.ToDateTime(cellsInRow[headerIndex].Text.ToString());
        break;
    }
}

if (columnHeader = "Create UTC Date")
{
    _createUTC = value;
}
else if (columnHeader = "Modify UTC Date")
{
    _modifyUTC = value;
}

Context

StackExchange Code Review Q#133953, answer score: 2

Revisions (0)

No revisions yet.