Forum Discussion

Brendachirata's avatar
Brendachirata
New Contributor
10 months ago

Finding children of children

Hello. So I have a root(Called CanvasContentHost, see the image, i highlighted it). Under this root there many children with the same name and same properties, I want the child called ItemsControl and its highlighted in the image. Under ItemsControl there many children with same properties(These Children are WPFObject("ContentPresenter", "", x) where x= 0, 1, ..., 13).  Under each child(WPFObject("ContentPresenter", "", x)),  I want to select a child called  WPFObject("ContentControl", "", 2), see the image for clarity. Under each of these children, i want to select a child WPFObject("Canvas", "", ), see image.I want to count the number of Children Under each of these children. For some there is 1 child and others there is 3.

 

NB in the properties Name is the only thing that make these objects unique and the Name : CanvasContentHost,  Name: WPFObject("ContentPresenter", "", x), Name: WPFObject("ContentControl", "", 2), etc

 

below is my code:

 

var canvas = Aliases.AutomationExpert.Root.root.SplitPane.MiddleWnd.PhysicalView.PartDiagramControl.CanvasContentHost;
var rack_inrackViewer = canvas.FindChild(['ClrClassName', 'ClrFullClassName', 'WPFControlOrdinalNo'], ['ItemsControl', 'System.Windows.Controls.ItemsControl', 1], 5);

var allSlots = rack_inrackViewer.FindAllChildren("Name", 'WPFObject("ContentControl", "", 2)', 5);

 

var canvasTwo = allSlots.FindAllChildren("Name", 'WPFObject("Canvas", "", 1)', 5);


for (let i in canvasTwo; i < canvasTwo.length; i++){

if (i.ChildrenCount=== 3){
Log.Message("slot" +" " + i +" " + "is an ethernet slot");
}
else {

Log.Message("slot" +" " + i +" " + "is not an ethernet slot");
}

}

3 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I suggest you read through FindAll Method. If you just want to count the number of children in a specific node, then you don't have to iterate through all parent-child (tree nodes).

  • rraghvani thank you for you answer. I have given more clarity to the question now. I have already used findAll and FindAllChildren Methods but it is not working. Seems as if it is not selecting everything

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Also read through Addressing Objects in WPF Applications.

     

    Here's an example that uses the FindAll method, and highlights only 5 buttons

     

    function FindButtons()
    {
        // Open https://www.w3schools.com/css/css3_buttons.asp
        var browser = Sys.Browser("chrome");
        var page = browser.Page("https://www.w3schools.com/css/css3_buttons.asp");
        var main = page.Panel("belowtopnav").Panel(0).Panel("main");
        
        var buttons = main.FindAll("ObjectType", "Button", 1);
        Log.Message("Number of buttons in page " + buttons.length);
        for (var i = 0; i < 5; i++) {
            Sys.HighlightObject(buttons[i]);
        }
    }

     

     

    FindAll method only returns an array of the objects, and doesn't perform anything else.