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

Looping Through and Pulling Values from a <UL> - Help?

7 Answers 528 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Gregory
Top achievements
Rank 1
Gregory asked on 16 Dec 2013, 04:08 PM
I am attemptin to cycle through an unordered list on a page, and then compare that to the values in the database.  So far, I am having trouble in cycling the <UL>.  Below is what I currently have:

foreach(Control item in Suites.Controls)
{
    if(item is HtmlGenericContol)
  {
      Log.WriteLine((HtmlGenericControl)item).InnerHtml + "<br>");
 }
}


I am getting a compile error of:

Line 79: (CS0103) The name 'Suites' does not exist in the current context

Can anyone point me in the right direction.  I will include a screen shot of the DOM for the list.

Any help would be appreciated!!

 

 

7 Answers, 1 is accepted

Sort by
0
Daniel
Top achievements
Rank 2
answered on 16 Dec 2013, 06:38 PM

Suites.Control...unless that is a reference made elsewhere to the unordered list, that is your issue.  The coded step has no idea what you are looking for.

HtmlUnorderedList suites = ActiveBrowser.Find.ById<HtmlUnorderedList>("Suites");
 
foreach(HtmlListItem item in suites.AllItems)
{
    Log.WriteLine(item.InnerText.ToString());  
}


The above finds the Unordered List via Find.ById and the ID, then loops through the List Items and logs the innertext.

Hope that helps.  If not, please explain a bit further and I'll do my best.

0
Gregory
Top achievements
Rank 1
answered on 16 Dec 2013, 07:05 PM
Daniel,

You're my hero.

That's exactly what it was.  Can't believe I missed that.  Thanks so much for your help!!
0
Boyan Boev
Telerik team
answered on 18 Dec 2013, 01:52 PM
Hello,

@Daniel, thank you again for your great help. I have updated your Telerik points.

@Gregory, if you need any additional assistance do not hesitate to contact us.

Thank you!

Regards,
Boyan Boev
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Gregory
Top achievements
Rank 1
answered on 18 Dec 2013, 05:45 PM
One final question that maybe someone can help with in regards to this.  Is there a way to parse out the child nodes of a <li>?  I have attached two files:  one is the UI and one is the DOM.  The <li> has multiple child nodes:

<ul class="nav nav-pills" id="SuiteList">
<li>
<a href="/NervStaging/Suite/Detail/1">
<section class="panel clearfix m-b-sm" style="width: 250px; height: 100px;">
<div class="panel-body">
<div class="clear">
<small class="block text-info">Alf</small>
<small class="block text-muted">Sche</small></div></div></section></a></li

When i pull the .innertext of the item, the output pulls the values for:
<small class="block text-info">Alf</small>
<small class="block text-muted">Sche</small></div></div></section></a></li

In this case, I only want the "block text-info" value of Alf.

The value of itemString below returns "AlfSche":

HtmlUnorderedList suitelist = ActiveBrowser.Find.ById<HtmlUnorderedList>("SuiteList");

var itemCount = 0;

var itemString = String.Empty;

 

//Loop through each <li>, verify, and create a string to pass to SQL query:

foreach(HtmlListItem item in suitelist.AllItems)

{

   if(!item.CssClass.Contains("hide"))

    {       

        itemCount = (itemCount + 1);

        itemString = itemString + "," + ("\"" + item.InnerText.ToString() + "\"");

        Log.WriteLine(itemString);

       

    }

    itemString = itemString.TrimStart(',');   

}

Is there a way to parse this differently to get just the "block text-info" value of Alf?  Should we have the developers assign an ID to this field to make it easier?

Thanks again!!



0
Daniel
Top achievements
Rank 2
answered on 18 Dec 2013, 06:50 PM

Adding another find element in your look can give you the text of just that class....

HtmlUnorderedList suites = ActiveBrowser.Find.ById<HtmlUnorderedList>("Suites");
 
foreach(HtmlListItem item in suites.AllItems)
{
    Element e = item.Find.ByAttributes("class='block text-info'");
    Log.WriteLine(e.InnerText.ToString());
    //Log.WriteLine(item.InnerText.ToString());
}

Here we are using essentially the same code, just adding a Find inside the loop.  Because there isn't a direct wrapper that I know of for the <small> tag, we use the generic Element and find by the class.  Once we've found the <small> tag inside the <li> tag, we can log the innertext.

InnerText will find all the text inside the element (even nested elements), which is why you were seeing the scrunched version of the available text.  Check out InnerText, TextContent, OuterMarkup, and InnerMarkup.  These will generally give you the content you are looking for in each element.

0
Daniel
Top achievements
Rank 2
answered on 18 Dec 2013, 06:52 PM
Oh, and always have the developers assign IDs! :)  Doesn't matter if you don't need them now, you may in the future.
0
Boyan Boev
Telerik team
answered on 20 Dec 2013, 01:57 PM
Hello,

Absolutely agree with Daniel about the IDs. As long as the IDs are unique, your element repository will not require maintenance when changes are made to your application.

You can check out this article.

Regards,
Boyan Boev
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Gregory
Top achievements
Rank 1
Answers by
Daniel
Top achievements
Rank 2
Gregory
Top achievements
Rank 1
Boyan Boev
Telerik team
Share this question
or