This is a migrated thread and some comments may be shown as answers.

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

1 Answer 48 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Randall
Top achievements
Rank 1
Randall asked on 12 Sep 2018, 02:47 PM

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

1 Answer, 1 is accepted

Sort by
0
Accepted
Nikolai
Telerik team
answered on 14 Sep 2018, 06:21 AM
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!
Tags
General Discussions
Asked by
Randall
Top achievements
Rank 1
Answers by
Nikolai
Telerik team
Share this question
or