使用Windows自动化查找和调用没有祖先的button

我们正尝试在Lync界面中自动点击以下button。 问题是我们无法可靠地点击它。 有时候它有效。 其他时间没有。 我们正在努力完成的是打开Lync选项窗口。

以下是问题的一些部分:

  • 我们使用System.Windows.Automation命名空间进行UI自动化。
  • 通常我们调用AutomationElement.FindFirst来获取UI元素
  • 这对button不起作用,因为button的菜单没有任何祖先
  • 所以,我们使用TreeWalker来遍历AutomomationElement.RootElement所有后代
  • 使用TreeWalker ,我们有时find一个匹配,但奇怪,不可预测的,我们有时不会
  • 使用Inspect.exe我们可以看到button的“上下文”菜单没有任何祖先

我们如何可靠地find并点击button?

厄运的上下文菜单。

Inspect.exe

从Inspect.exe

 How found: Selected from tree... Name: "" ControlType: UIA_MenuItemControlTypeId (0xC35B) LocalizedControlType: "menu item" ProcessId: 0 IsControlElement: true IsContentElement: true ProviderDescription: "[pid:3084,hwnd:0x0 Annotation:Microsoft: Annotation Proxy (unmanaged:UIAutomationCore.dll); Main(parent link):Microsoft: MSAA Proxy (unmanaged:UIAutomationCore.dll)]" IsPeripheral: [Not supported] LiveSettingProperty: [Not supported] FlowsFrom: [Not supported] OptimizeForVisualContent: [Not supported] Annotation.AnnotationAuthor: [Not supported] Annotation.AnnotationTypeId: [Not supported] Annotation.Author: [Not supported] Annotation.DateTime: [Not supported] Annotation.Target: [Not supported] Drag.DropEffect: [Not supported] Drag.DropEffects: [Not supported] Drag.GrabbedItems: [Not supported] Drag.IsGrabbed: [Not supported] DropTarget.DropTargetEffect: [Not supported] DropTarget.DropTargetEffects: [Not supported] LegacyIAccessible.ChildId: 3 LegacyIAccessible.Name: "" ObjectModel.UnderlyingObjectModel: [Error: calling getter for this property: hr=0x80070057 - The parameter is incorrect.] SpreadsheetItem.AnnotationObjects: [Not supported] SpreadsheetItem.AnnotationTypes: [Not supported] SpreadsheetItem.Formula: [Not supported] Style.ExtendedProperties: [Not supported] Style.FillColor: [Not supported] Style.FillPatternColor: [Not supported] Style.FillPatternStyle: [Not supported] Style.Shape: [Not supported] Style.StyleId: [Not supported] Style.StyleName: [Not supported] Transform2.CanZoom: [Not supported] Transform2.ZoomLevel: [Not supported] Transform2.ZoomMinimum: [Not supported] Transform2.ZoomMaximum: [Not supported] IsAnnotationPatternAvailable: [Not supported] IsDragPatternAvailable: [Not supported] IsDockPatternAvailable: false IsDropTargetPatternAvailable: [Not supported] IsExpandCollapsePatternAvailable: false IsGridItemPatternAvailable: false IsGridPatternAvailable: false IsInvokePatternAvailable: false IsItemContainerPatternAvailable: false IsLegacyIAccessiblePatternAvailable: true IsMultipleViewPatternAvailable: false IsObjectModelPatternAvailable: [Not supported] IsRangeValuePatternAvailable: false IsScrollItemPatternAvailable: false IsScrollPatternAvailable: false IsSelectionItemPatternAvailable: false IsSelectionPatternAvailable: false IsSpreadsheetItemPatternAvailable: [Not supported] IsSpreadsheetPatternAvailable: [Not supported] IsStylesPatternAvailable: [Not supported] IsSynchronizedInputPatternAvailable: false IsTableItemPatternAvailable: false IsTablePatternAvailable: false IsTextChildPatternAvailable: [Not supported] IsTextEditPatternAvailable: [Not supported] IsTextPatternAvailable: false IsTextPattern2Available: [Not supported] IsTogglePatternAvailable: false IsTransformPatternAvailable: false IsTransform2PatternAvailable: [Not supported] IsValuePatternAvailable: false IsVirtualizedItemPatternAvailable: false IsWindowPatternAvailable: false FirstChild: [null] LastChild: [null] Next: [null] Previous: [null] Other Props: Object has no additional properties Children: [Error: calling getter for this property: hr=0x80004005 - Unspecified error] Ancestors: "Context" menu "Context" window [ No Parent ] 

如何通过UIA打开下拉菜单,然后使用SendKeys发送Alt-D(“%d”)?