0

I have multiple tables and Location Value is given in different index order. How can I get location value if previous cell string is "Location" when I loop through table. On below example it is cells[7] but on other table it will be 9. How can I conditionally get values after cells inner text is "Location"? Basically find the cell "Location" get inner text of next cell.

Html Table:

<table class="tbfix FieldsTable"">
<tbody>
    <tr>
        <td class="name">Last Movement</td>
        <td class="value">Port Exit</td>
    </tr>
    <tr>
        <td class="name">Date</td>
        <td class="value">26/06/2017 00:00:00</td>
    </tr>
    <tr>
        <td class="name">From</td>
        <td class="value">HAMBURGE</td>
    </tr>
    <tr>
        <td class="name">Location</td>
        <td class="value">EUROGATE  HAMBURG</td>
    </tr>
    <tr>
        <td class="name">E/F</td>
        <td class="value">E</td>
    </tr>
</tbody>

Controller Loop Through:

foreach (var eachNode in driver.FindElements(By.XPath("//table[contains(descendant::*, 'Last Movement')]")))
                {
                    var cells = eachNode.FindElements(By.XPath(".//td"));
                    cd = new Detail();

                    for (int i = 0; i < cells.Count(); i++)
                    {
                        cd.ActionType = cells[1].Text.Trim();
                        string s = cells[3].Text.Trim();
                        DateTime dt = Convert.ToDateTime(s);
                        if (_minDate > dt) _minDate = dt;
                        cd.ActionDate = dt;

                    }
                }

2 Answers 2

1

In your foreach loop you could use this:

var location = eachNode.FindElement(By.XPath(".//td[contains(text(),'Location')]/following-sibling::td));
1

Assuming your data is always structured like that I would loop over all the tags and add the data to a dictionary.

Try something like this:

Dictionary<string,string> tableData = new Dictionary<string, string>();
var trNodes = eachNode.FindElements(By.TagName("tr"));

foreach (var trNode in trNodes)
{
    var name = trNode.FindElement(By.CssSelector(".name")).Text.Trim();
    var value = trNode.FindElement(By.CssSelector(".value")).Text.Trim();

    tableData.Add(name,value);
}

var location = tableData["location"];

You would have to add validation and checks for the dictionary and the structure but that is the general idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.