Element.ChildCount returns wrong number

Bug reports.
chstnq
Posts: 9
Joined: Tue May 20, 2008 7:59 am

Element.ChildCount returns wrong number

Post by chstnq » Tue May 20, 2008 8:20 am

I want to find out the number of items on a list using the following code:

Code: Select all

..
int i = 0;
control = form.FindControlId(31);
control.Focus();
controlElement = control.Element;
// Find List
Element listElement = controlElement.FindChild(Role.List);
if (listElement != null)
{
i = listElement.ChildCount;
}
return i;
..
the returned value is wrong and always [expected value] + 1

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Post by Support Team » Tue May 20, 2008 9:21 am

Actually, that's not a bug. The thing is that not all children of a list element are list items. Usually, there is another child element of role 'Window'. That's why you always get a ChildCount of [number of list items] + 1.

You can use the FindChildren method to search for all items in a list element:

Code: Select all

Element[] listItems = listElement.FindChildren(Role.ListItem);
return listItems.Length;
...
Regards,
Alex
Ranorex Support Team