UI Automation Control桌面应用程序,然后单击菜单栏

时间:2019-05-23 07:02:25

标签: c# winforms menu ui-automation desktop-application

我正在使用UI Automation来单击菜单栏控件。 我已经预填充了文本框,现在也可以调用该按钮。

但是我想遍历菜单栏,选择一个选项,说“文件”,它应该打开其子菜单项,然后单击一个子菜单按钮,说“退出”。

我如何实现它,以下是到目前为止的代码,

 AutomationElement rootElement = AutomationElement.RootElement;
            if (rootElement != null)
            {

                System.Windows.Automation.Condition condition = new PropertyCondition
             (AutomationElement.NameProperty, "This Is My Title");
              rootElement.FindAll(TreeScope.Children, condition1);
                AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);


                if (appElement != null)
                {
                    foreach (var el in eles)
                    {
                        AutomationElement txtElementA = GetTextElement(appElement, el.textboxid);
                        if (txtElementA != null)
                        {
                            ValuePattern valuePatternA =
                            txtElementA.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
                            valuePatternA.SetValue(el.value);
                            el.found = true;
                        }

                    }


                    System.Threading.Thread.Sleep(5000);
                    System.Windows.Automation.Condition condition1 = new PropertyCondition
                    (AutomationElement.AutomationIdProperty, "button1");
                    AutomationElement btnElement = appElement.FindFirst
                            (TreeScope.Descendants, condition1);

                    InvokePattern btnPattern =
                    btnElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                    btnPattern.Invoke();

1 个答案:

答案 0 :(得分:0)

菜单项支持ExpandCollapsePattern。展开后可以调用子# The Miliseconds column is not currently in milliseconds, it is # a fraction of a second (0.016) # Rounding it off is easy df["Miliseconds"] = df["Miliseconds"].round(3) # Make it milliseconds, not fraction of a seconds df["Miliseconds"] = df["Miliseconds"] * 1000 # Now add it to Time using a Timedelta tdelta = pd.to_timedelta(df["Miliseconds"], unit="ms") df["Time1"] = df["Time"] + tdelta 。这将创建MenuItem后代对象。如果不展开菜单,则它没有后代,因此没有什么可调用的。
调用是使用InvokePattern

执行的

要获取 MenuItem ExpandCollapsePattern ,请使用TryGetCurrentPattern方法:

InvokePattern

如果该方法返回成功的结果,则可以调用Expand()Invoke()方法。

请注意,MenuBar元素具有[MenuItem].TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out object pattern) [MenuItem].TryGetCurrentPattern(InvokePattern.Pattern, out object pattern) ,而MenuItem具有Children。如果您使用FindAll()方法来搜索子级,则不会找到任何子级。

  

在编写UI自动化代码时,Inspect utility非常有用   程序。通常位于:

Descendants
     

有32位版本(C:\Program Files (x86)\Windows Kits\10\bin\x64\inspect.exe 文件夹)。

如何进行:

  • 找到要与之交互的应用程序主窗口的句柄:
  • 获取 \bin\x86\ AutomationElement。
    • 请注意,MenuBar也包含在SystemMenu中,可以使用元素名称来确定,它包含:MenuBar而不是"System"
  • 按名称枚举"Application"FindFirst()的子元素。
    • 请注意,菜单项名称和加速器已本地化。
  • 使用 MenuBar 方法扩展菜单,以创建其Descendants元素。
  • 通过 ExpandCollapsePattern.Expand() Name AutomationId 查找子菜单元素之一。
  • 使用 AccessKey 方法调用子菜单操作。

当然,可以重复相同的操作来扩展和调用嵌套子菜单的MenuItems。

示例代码和帮助程序方法,以查找和展开InvokePattern.Invoke()的文件菜单并调用 Notepad.exe MenuItem操作:

Exit