UIAutomation - InvokePattern 仅在调试 - 按钮未被识别为按钮时起作用

本文关键字:按钮 识别 起作用 调试 UIAutomation InvokePattern | 更新日期: 2023-09-27 18:30:14

我正在使用UIAutomation操纵一个旧程序。我遇到的问题涉及使用 InvokePattern 按下按钮。

按下打开资源管理器窗口的按钮并获取窗口的AutomationElement后,我得到了"打开"按钮的AutomationELement,实际上是SplitButton。我可以轻松找到该元素,但它作为 Pane 控件而不是SplitButton控件出现。但是,如果我在查找 Button 元素之前插入断点并在调试模式下手动单步执行代码,则"打开"按钮将被识别为按钮。

如果我在找到Button元素后插入断点,则元素 Name 和 AutomationID是正确的,但ControlType是窗格而不是按钮。如果我在获取资源管理器窗口后放置延迟也没关系,它仅在调试时有效。这很奇怪。

违规代码如下:

InvokePattern bPattern = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
        bPattern.Invoke();
        for (int wait = 0; wait < 50; wait++)
        {
            if (explorerWindow != null)
                break;
            explorerWindow = reportWindow.FindFirst(TreeScope.Children,
                                            new PropertyCondition(AutomationElement.NameProperty, "Select Report"));
            Thread.Sleep(200);
        }
        explorerOpenButton = explorerWindow.FindFirst(TreeScope.Children,
                                new PropertyCondition(AutomationElement.NameProperty, "Open"));

UIAutomation - InvokePattern 仅在调试 - 按钮未被识别为按钮时起作用

尝试创建一个包含两个PropertyConditionAndCondition,一个用于按钮的名称,另一个用于按钮的类名(SplitButton ?)。将该逻辑放在 while/for 循环中,就像您在寻找窗口时所做的那样。这样:

var andCondition = new AndCondition(new PropertyCondition(AutomationElement.NameProperty, "Open"), new PropertyCondition(AutomationElement.ClassNameProperty, "SplitButton"));

我认为有可能有两个具有相同名称但具有不同类名的元素以某种方式出现在可视化树的同一层次结构中。

如果这没有帮助,请尝试以相同的方式查询 RuntimeId 属性,看看它是否返回。