0

I am looking for a method to "click" a toolbar button in another application using the UIAutomation namespace. The other application is not written by me and I don't have access to the source.

I found the parent window using:

AutomationElement _automationElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Other App"));

I found the toolbar element using:

AutomationElement _toolbarElement = _automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "ToolBar1"));

I've tried to navigate further into the toolbar element for decendents and children and they all returned null. Is there a way to access the individual buttons contained within the toolbar?

Dan
  • 23
  • 4
  • Use a tool such as "Inspect" https://learn.microsoft.com/en-us/windows/win32/winauto/inspect-objects or "Accessibility Insights" https://accessibilityinsights.io/ to first check what elements you can get to using UI Automation. If you can see the element you're after, then you can get to it programmatically otherwise you can't – Simon Mourier Jun 08 '23 at 16:10
  • OK, thanks, that answers my question. I was using Accessibility Insights and it shows the entire toolbar as one object. I guess you can't drill down to the individual buttons of the toolbar. I was hoping there was something simple that I was missing. – Dan Jun 09 '23 at 16:59

1 Answers1

0

I'm working on a similar project, in my case I used this

    private AutomationElement FindButton(AutomationElement parent, string buttonName)
    {
        AutomationElementCollection list = parent.FindAll(TreeScope.Descendants,
               new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));
        foreach (AutomationElement element in list)
        {
            if (!string.IsNullOrEmpty(element.Current.Name) && element.Current.Name.Equals("button_name"))
            {
                return element;
            }
        }
        return null;
    }

to find all visible buttons and then put in a variable the need one, however I'm still seeking an adequate method to do the click. At this link there's a method but until now I didn't found the right Mouse class to use. If you can instead, please share.

EDIT: In the end I preferred another approach that worked pretty well

    private void ClickButton(AutomationElement button)
    {
        InvokePattern pat = button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
        pat.Invoke();
    }

The only thing I had to check is if the button have IsInvokePatternAvailable: true with Inspect.exe (part of Windows SDK)

LM_IT
  • 168
  • 5
  • This isn't quite the same thing as my situation....I'm dealing with a toolbar object that doesn't have descendents. If I run a "AutomationElement.FindAll" it will not return any descendents or children. – Dan Jun 29 '23 at 15:25