[英]Using Windows.Automation, can I locate an AutomationElement by regex?
我有一个对象树,在表父级中有行对象。 我正在尝试将所有这些行放入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
控制查找范围。
如文档所述 ,您可以要求不区分大小写的比较。 没有“正则表达式”标志。 您将必须手动进行过滤。
//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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.