• Result 1

    How can I get the children of the item in batting?

    Given the following html:

    <ul id="search-results"> <li> <h3>Result 1</h3> <span class="some-info">Tag line</span> </li> <li> <h3>Result 2</h3> <span class="some-info">Another bit</span> </li> <li> <h3>Result 2</h3> <span class="some-info">Another bit</span> </li> </ul> 

    I can get the ul element with:

     Element ul = ie.Element(Find.ById("search-results")); 

    How to iterate over child elements of search-results ?

    I managed to get to:

     var allListItems = ie.Elements.Filter(e => e.Parent != null && e.Parent.Id == "search-results"); 

    But this does not help me assert things about H3 or span contained in li .

    +6
    c # watin
    source share
    2 answers

    * Option1: * Based on your code, you can try something like:

     ie.Spans.Filter(Find.ByClass("some-info"))[0...2].PreviousSibling; 

    you will need to iterate over each interval.

    * Option 2: * Based on some suggestions from the Watin mail list (similar to your own answer):

     IElementContainer elem = (IElementContainer)ie.Element(Find.ById("search-results")); 

    Then you parse elem.Element

    +8
    source share

    The current best hack I can think of:

     public static class WatinExtensions { public static ElementCollection Children(this Element self) { return self.DomContainer.Elements.Filter(e => self.Equals(e.Parent)); } } ElementCollection results = ie.Element(Find.ById("search-results")).Children(); // and foreach(Element li in results) { Element info = li.Children().First(e => e.ClassName.Contains("some-info")); } 

    It works, but is there definitely the right solution for this?

    And there is the right way to do this from the watin mailing list:

    In my example:

     var searchResults = (IElementContainer) ie.Element(Find.ById("search-results")); foreach (Element element in searchResults.Elements) { // can do more here } 
    +7
    source share

    All Articles