Export (0) Print
Expand All

Find a UI Automation Element Based on a Property Condition

Note Note

This documentation is intended for .NET Framework developers who want to use the managed UI Automation classes defined in the System.Windows.Automation namespace. For the latest information about UI Automation, see Windows Automation API: UI Automation.

This topic contains example code that shows how to locate an element within the UI Automation tree based on a specific property or properties.

In the following example, a set of property conditions are specified that identify a certain element (or elements) of interest in the AutomationElement tree. A search for all matching elements is then performed with the FindAll method that incorporates a series of AndCondition boolean operations to limit the number of matching elements.

Note Note

When searching from the RootElement, you should try to obtain only direct children. A search for descendants might iterate through hundreds or even thousands of elements, possibly resulting in a stack overflow. If you are attempting to obtain a specific element at a lower level, you should start your search from the application window or from a container at a lower level.

        ''' <summary> 
        ''' Walks the UI Automation tree of the target and reports the control  
        ''' type of each element it finds in the control view to the client. 
        ''' </summary> 
        ''' <param name="targetTreeViewElement"> 
        ''' The root of the search on this iteration. 
        ''' </param> 
        ''' <param name="treeviewIndex"> 
        ''' The TreeView index for this iteration. 
        ''' </param> 
        ''' <remarks> 
        ''' This is a recursive function that maps out the structure of the  
        ''' subtree beginning at the AutomationElement passed in as  
        ''' rootElement on the first call. This could be, for example, 
        ''' an application window. 
        ''' CAUTION: Do not pass in AutomationElement.RootElement. Attempting  
        ''' to map out the entire subtree of the desktop could take a very  
        ''' long time and even lead to a stack overflow. 
        ''' </remarks> 
        Private Sub FindTreeViewDescendants( _
        ByVal targetTreeViewElement As AutomationElement, _
        ByVal treeviewIndex As Integer)
            If (IsNothing(targetTreeViewElement)) Then 
            End If 

            Dim elementNode As AutomationElement = _

            While Not (elementNode Is Nothing)
                Dim elementInfo As New Label()
                elementInfo.Margin = New Thickness(0)

                ' Compile information about the control.
                elementInfoCompile = New StringBuilder()
                Dim controlName As String 
                If (elementNode.Current.Name = "") Then
                    controlName = "Unnamed control" 
                    controlName = elementNode.Current.Name
                End If 
                Dim autoIdName As String 
                If (elementNode.Current.AutomationId = "") Then
                    autoIdName = "No AutomationID" 
                    autoIdName = elementNode.Current.AutomationId
                End If

                elementInfoCompile.Append(controlName).Append(" (") _
                .Append(elementNode.Current.ControlType.LocalizedControlType) _
                .Append(" - ").Append(autoIdName).Append(")")

                ' Test for the control patterns of interest for this sample. 
                Dim objPattern As Object = Nothing 
                Dim expcolPattern As ExpandCollapsePattern
                If True = elementNode.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, objPattern) Then
                    expcolPattern = DirectCast(objPattern, ExpandCollapsePattern)
                    If expcolPattern.Current.ExpandCollapseState <> ExpandCollapseState.LeafNode Then 
                        Dim expcolButton As New Button()
                        expcolButton.Margin = New Thickness(0, 0, 0, 5)
                        expcolButton.Height = 20
                        expcolButton.Width = 100
                        expcolButton.Content = "ExpandCollapse"
                        expcolButton.Tag = expcolPattern
                        AddHandler expcolButton.Click, AddressOf ExpandCollapse_Click
                    End If 
                End If 
                Dim togPattern As TogglePattern
                If True = elementNode.TryGetCurrentPattern(TogglePattern.Pattern, objPattern) Then
                    togPattern = DirectCast(objPattern, TogglePattern)
                    Dim togButton As New Button()
                    togButton.Margin = New Thickness(0, 0, 0, 5)
                    togButton.Height = 20
                    togButton.Width = 100
                    togButton.Content = "Toggle"
                    togButton.Tag = togPattern
                    AddHandler togButton.Click, AddressOf Toggle_Click
                End If 
                Dim invPattern As InvokePattern
                If True = elementNode.TryGetCurrentPattern(InvokePattern.Pattern, objPattern) Then
                    invPattern = DirectCast(objPattern, InvokePattern)
                    Dim invButton As New Button()
                    invButton.Margin = New Thickness(0)
                    invButton.Height = 20
                    invButton.Width = 100
                    invButton.Content = "Invoke"
                    invButton.Tag = invPattern
                    AddHandler invButton.Click, AddressOf Invoke_Click
                End If 
                ' Display compiled information about the control.
                elementInfo.Content = elementInfoCompile
                Dim sep As New Separator()

                ' Iterate to next element. 
                ' elementNode - Current element. 
                ' treeviewIndex - Index of parent TreeView.
                FindTreeViewDescendants(elementNode, treeviewIndex)
                elementNode = TreeWalker.ControlViewWalker.GetNextSibling(elementNode)
            End While 

        End Sub 'FindTreeViewDescendants
© 2014 Microsoft