System.Windows.Automation.AutomationElement.FindFirst C# (CSharp) Method

FindFirst() public method

public FindFirst ( TreeScope scope, Condition condition ) : AutomationElement
scope TreeScope
condition Condition
return AutomationElement
        public AutomationElement FindFirst(TreeScope scope, Condition condition)
        {
            Utility.ValidateArgumentNonNull(condition, "condition");
            try
            {
                UIAutomationClient.IUIAutomationElement elem =
                    this._obj.FindFirstBuildCache(
                        (UIAutomationClient.TreeScope)scope,
                        condition.NativeCondition,
                        CacheRequest.CurrentNativeCacheRequest);
                return AutomationElement.Wrap(elem);
            }
            catch (System.Runtime.InteropServices.COMException e)
            {
                Exception newEx; if (Utility.ConvertException(e, out newEx)) { throw newEx; } else { throw; }
            }
        }

Usage Example

Example #1
0
        private static bool ButtonClick(AutomationElement inElement, string automationId)
        {
            PropertyCondition btnCondition = new PropertyCondition(AutomationElement.AutomationIdProperty, automationId);

            Console.WriteLine("Searching for the {0} button...", automationId);
            AutomationElement control = inElement.FindFirst(TreeScope.Descendants, btnCondition);
            if (control != null)
            {
                Console.WriteLine("Clicking the {0} button", automationId);

                object controlType = control.GetCurrentPropertyValue(AutomationElement.ControlTypeProperty);
                if (controlType == ControlType.Button)
                {
                    InvokePattern clickCommand = control.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                    clickCommand.Invoke();
                }
                else if (controlType == ControlType.RadioButton)
                {
                    SelectionItemPattern radioCheck = control.GetCurrentPattern(SelectionItemPattern.Pattern) as SelectionItemPattern;
                    radioCheck.Select();
                }
                System.Threading.Thread.Sleep(2000);
                Console.WriteLine("Button {0} clicked.", automationId);

                return true;
            }
            else
            {
                Console.WriteLine("Could not find button {0} ", automationId);
                return false;
            }
        }
All Usage Examples Of System.Windows.Automation.AutomationElement::FindFirst