【问题标题】:UIAutomation - InvokePattern only works while Debugging - Button is not Recognized as a ButtonUIAutomation - InvokePattern 仅在调试时有效 - 按钮未被识别为按钮
【发布时间】:2013-09-09 14:16:05
【问题描述】:

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

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

如果我在找到Button 元素后插入断点,则元素名称和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"));

【问题讨论】:

    标签: c# .net winforms automation ui-automation


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 不用类,只用IsInvokePatternAvailableProperty-new AndCondition(new PropertyCondition(AutomationElement.NameProperty, "Open"), new PropertyCondition(AutomationElement.IsInvokePatternAvailableProperty, true));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多