Search code examples
c#htmlparsingwatin

Retrieve element within html hierarchy


I have this piece of html code. I want to get the text inside the <div> tag using WatiN. The C# code is below, but I'm pretty sure it could be done way better than my solution. Any suggestions?

HTML:

<table id="someId" cellspacing="0" border="1" style="border-collapse:collapse;" rules="all">
    <tbody>
        <tr>
            <th scope="col">&nbsp;</th>
        </tr>
        <tr>
            <td>
                <div>Some text</div>
            </td>
        </tr>
    </tbody>
</table>

C#

// Get the table ElementContainer
IElementContainer diagnosisElementContainer = (IElementContainer)_control.GetElementById("someId");

// Get the tbody element
IElementContainer tbodyElementContainer = (IElementContainer)diagnosisElementContainer.ChildrenWithTag("tbody");

// Get the <tr> children
ElementCollection trElementContainer = tbodyElementContainer.ChildrenWithTag("tr");

// Get the <td> child of the last <tr>
IElementContainer tdElementContainer = (IElementContainer)trElementContainer.ElementAt<Element>(trElementContainer.Count - 1);

// Get the <div> element inside the <td>
Element divElement = tdElementContainer.Divs[0];

Solution

  • Based on the given, something like this is how I'd go for IE.

    IE myIE = new IE();
    myIE.GoTo("[theurl]");
    string theText = myIE.Table("someId").Divs[0].Text;
    

    The above is working on WatiN 2.1, Win7, IE9.