【问题标题】:Using Windows.Automation, can I locate an AutomationElement by regex?使用 Windows.Automation,我可以通过正则表达式找到 AutomationElement 吗?
【发布时间】:2013-01-30 19:21:30
【问题描述】:

我有一个对象树,它在父表中包含行对象。我正在尝试将所有这些行放入 AutomationElementCollection

AutomationElementCollection asdf = ParentTableObj.FindAll
     (
     TreeScope.Children,
     new PropertyCondition
          (
          AutomationElement.NameProperty,
          "I want to use regex here"
          )
     );

所有行的AutomationElement.NameProperty 都包含字符串“row”。但是,它们是该字符串的变体 - 例如“Row1”、“Row2”、“TopRow”、...

似乎我可能遗漏了一些东西,因为FindAll 方法允许您定义TreeScope 并找到与提供的Condition 参数匹配的任何AutomationElement。我只是希望我的条件不受限制,因为我已经可以通过TreeScope 控制查找范围。

【问题讨论】:

    标签: c# windows automation ui-automation automationelement


    【解决方案1】:
    //Example :
    AutomationElement element = FindFirstDescendant( 
        AutomationElement.FromHandle(windows_hWnd), 
        (ele)=>Regex.IsMatch( ele.Current.Name, pattern)
    );
    
    //The generic method to find a descendant element:
    public static AutomationElement FindFirstDescendant(AutomationElement element, Func<AutomationElement, bool> condition) {
        var walker = TreeWalker.ControlViewWalker;
        element = walker.GetFirstChild(element);
        while (element != null) {
            if (condition(element))
                return element;
            var subElement = FindFirstDescendant(element, condition);
            if (subElement != null)
                return subElement;
            element = walker.GetNextSibling(element);
        }
        return null;
    }
    

    【讨论】:

    • FindDescendant 似乎没有定义
    • @John Smith,我更新了答案,名称已重命名为 FindFirstDescendant。
    【解决方案2】:

    As the documentation states,您可以要求进行不区分大小写的比较。没有“正则表达式”标志。您必须手动进行过滤。

    【讨论】:

    • 我认为可能是这种情况 - 感谢您的澄清!
    猜你喜欢
    • 1970-01-01
    • 2010-09-29
    • 2012-04-14
    • 2015-03-21
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多