There is a program written in .net.

Program menu:

How can I get a list of these elements?

In the AutomationTree menu has no children:

Code:

void getChildrenNodes(AutomationElement aElement, TreeNode mainNode) { AutomationElementCollection aEColl = aElement.FindAll(TreeScope.Children, Condition.TrueCondition); foreach (AutomationElement item in aEColl) { TreeNode childNode = new TreeNode(getElementText(item)); mainNode.Nodes.Add(childNode); getChildrenNodes(item, childNode); } } 

    1 answer 1

    The fact is that for the appearance of child-elements of the menu you need to open.

    This code works for me here:

     var process = Process.GetProcessesByName(name).FirstOrDefault(); var window = AutomationElement.FromHandle(process.MainWindowHandle); // вызовем Файл -> Выход (на англоязычной системе понадобятся другие строки!) var menuBar = window.FirstChildByType(ControlType.MenuBar); var fileMenu = menuBar.FirstDescendantByTypeAndName(ControlType.MenuItem, "Файл"); // раскрыли меню File: fileMenu.GetPattern<ExpandCollapsePattern>().Expand(); // подождём, пока меню раскроется реально Thread.Sleep(100); // нашли пункт Выход var exitMenu = fileMenu.FirstDescendantByTypeAndName(ControlType.MenuItem, "Выход"); // и выполнили его exitMenu.GetPattern<InvokePattern>().Invoke(); Thread.Sleep(100); 

    Helper class (taken from here ):

     static class AutomationHelpers { static public T GetPattern<T>(this AutomationElement element) where T : BasePattern { var pattern = (AutomationPattern)typeof(T).GetField("Pattern").GetValue(null); return (T)element.GetCurrentPattern(pattern); } static public AutomationElement FirstChildByType( this AutomationElement element, ControlType ct) { return element.FindFirst( TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ct)); } static public AutomationElement FirstDescendantByTypeAndName( this AutomationElement element, ControlType ct, string name) { return element.FindFirst( TreeScope.Descendants, new AndCondition( new PropertyCondition(AutomationElement.ControlTypeProperty, ct), new PropertyCondition(AutomationElement.NameProperty, name))); } static public AutomationElement FindWindowFrom(AutomationElement control) { var walker = TreeWalker.ControlViewWalker; while (control.Current.ControlType != ControlType.Window) control = walker.GetParent(control); return control; } }