识别UI元素
找到窗口就可以开始找窗口上的UI元素了。
比如我想找Calculator上的文本框
可以用如下代码实现:
- using System.Windows.Automation;
- ...
- //找到Desktop
- AutomationElement Desktop = AutomationElement.RootElement;
- //找到Calculator窗口
- AutomationElement CalcWindows = Desktop.FindFirst(TreeScope.Children,
- new AndCondition(
- new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
- new PropertyCondition(AutomationElement.ClassNameProperty, "SciCalc")
- )
- );
- //找到Calculator的Edit控件
- AutomationElement CalcEdit=CalcWindows.FindFirst(TreeScope.Children,
- new AndCondition(
- new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
- new PropertyCondition(AutomationElement.ClassNameProperty, "Edit")
- )
- );
AutomationElement.FindFirst 的方法
public AutomationElement FindFirst( TreeScope scope, Condition condition)
TreeScope是个枚举类型
成员名称 | 说明 |
Element | 指定搜索包括元素本身。 |
Children | 指定搜索包括元素的直接子级。 |
Descendants | 指定搜索包括元素的子代(包括子级)。 |
Parent | 指定搜索包括元素的父级。不支持。 |
Ancestors | 指定搜索包括元素的上级(包括��级)。不支持。 |
Subtree | 指定搜索包括搜索的根和全部子代。 |
Children和Descendants比较常用。Subtree也用比较多。
Condition 类
在 UI 自动化目录树中搜索元素时应用于筛选的条件的基类型。
主要都使用下面几个子类
System.Windows.Automation.AndCondition System.Windows.Automation.NotCondition System.Windows.Automation.OrCondition System.Windows.Automation.PropertyCondition
AndCondition 表示一个与(And)条件 NotCondition 表示一个非(Not)条件
OrCondition 表示一个或(Or)条件
PropertyCondition 它测试属性是否具有指定的值
可以在UISpy的右边Identification中找到相应的值。
一般用ControlType,ClassName, Automationid 和 Name 就够用了。其他的一般较少使用。
转载来源:http://blog.csdn.net/vbic0673/article/details/6093320