How to iterate over a collection of elements and check the inner text?

2 posts, 1 answers
  1. Randall
    Randall avatar
    1 posts
    Member since:
    Sep 2018

    Posted 12 Sep Link to this post

    I am new to TestStudio and I have been looking around the application and online to be able to iterate over a collection of elements and get the inner text from all of them to run them against an assertion. In ruby this can be done, but I'm not so sure about c#. 

     

    Thanks,

    Randall

  2. Answer
    Nikolai
    Admin
    Nikolai avatar
    106 posts

    Posted 14 Sep Link to this post

    Hi Randall,

    You can easily traverse the Html DOM following the "Parent - Child" recursive pattern:

    this.ActiveBrowser.NavigateTo("https://bing.com");
    this.ActiveBrowser.WaitUntilReady();
     
    //Enum div element children
    var divParent = this.ActiveBrowser.Find.ByCssSelector<HtmlDiv>(".b_searchboxForm");
     
    foreach(var child in divParent.BaseElement.Children)
    {
        Log.WriteLine("Div child: " + child.TagName);
    }
     
    //Enum elements starting from the DOM's root
    foreach(var child in this.ActiveBrowser.DomTree.Root.Children)
    {
         Log.WriteLine("Root child: " + child.TagName);
    }

    You can use any loop operators "foreach" , "for" etc. on the Element.Children collection and retrieve the child elements.

    Regards,
    Nikolai
    Progress Telerik
     
    The New Release of Telerik Test Studio Is Here! Download, install,
    and send us your feedback!
Back to Top