用UIAutomation识别UI元素(二)

识别UI元素

找到窗口就可以开始找窗口上的UI元素了。

 

比如我想找Calculator上的文本框

 

 可以用如下代码实现:

 

 

[c-sharp]  view plain copy
  1. using System.Windows.Automation;  
  2.   
  3.   
  4. ...  
  5. //找到Desktop  
  6. AutomationElement Desktop = AutomationElement.RootElement;  
  7. //找到Calculator窗口  
  8. AutomationElement CalcWindows = Desktop.FindFirst(TreeScope.Children,  
  9.                 new AndCondition(  
  10.                     new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),  
  11.                     new PropertyCondition(AutomationElement.ClassNameProperty, "SciCalc")  
  12.                     )  
  13.                     );  
  14. //找到Calculator的Edit控件  
  15. AutomationElement CalcEdit=CalcWindows.FindFirst(TreeScope.Children,  
  16.                new AndCondition(  
  17.                     new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),  
  18.                     new PropertyCondition(AutomationElement.ClassNameProperty, "Edit")  
  19.                     )  
  20.                     );  

 

 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据UIAutomation封装了很多自定义方法 现在只需要实例化之后 直接调用方法即可完成。比如单击某个按钮,现在只需要直接调用ClickElement,非常实用。 ClickElement 单击指定的自动化元素 DisselectAllDataGridRow 不选中所有行 DisselectDataGridRow 不选中特定的某一行 FocusWindow 获取窗口焦点 GetAllElement 获取指定父自动化元素下的所有激活的控件 GetAllElementDetails 获取指定自动化元素下的详细信息包括AutomationID,ControlType以及Name GetAllMenus 获取所有菜单项 GetAllSubMenus 获取某个菜单下的所有子菜单项 GetColumnsFromGridLine 获取指定行的所有列 GetColumnValuesFromGridLine 获取行的每一列数据 GetControlType(AutomationElement) 获取制动自动化元素的控件类型 GetControlType(TypeOfControl) 获取UIAutomation的控件类型 GetDocumentText 获取document控件的值 GetElementByID 获取父自动化元素下指定元素控件ID的引用 GetElementByName 获取父自动化元素下的指定子元素的引用 GetElementsByControlType 获取父自动化元素下的特定类型的所有自动化元素 GetGridLinesFromDataGrid 获取网格控件的全部行元素的引用 GetHeaderFromDataGrid 获取指定网格控件的标题栏引用 GetMenuBar 获取菜单栏控件 GetMenuByName 通过特定的名称去获取菜单UI自动化元素 GetName 获取指定自动化元素的名称 GetSubMenuByName 获取主菜单下的指定子菜单项的引用 GetValue 获取指定自动化元素的值 GetWindowByName(String) 获取desktop下的指定窗口名称的子UI自动化元素 GetWindowByName(String, AutomationElement) 获取特定父UI自动化元素下的制定窗口名称的子UI自动化元素 GetWindowList() 获取当前桌面根下所有的UI自动化元素下 GetWindowList(AutomationElement) 获取特定父UI自动化元素下的所有窗口的名称 RefindMainApplication 重新获取desktop下的指定窗口的自动化元素引用 SelectAllDataGridRow 选中所有行 SelectDataGridRow(AutomationElement) 选中特定的某一行 SelectDataGridRow(AutomationElement, Boolean) 将特定的DateGridRow加入选中项中 SelectValueInComboBox 从下拉框中选中指定值的项 SelectValueInListBox 从列表中选中指定值的项 SetValue 给予指定自动化元素赋值 以上的方法还不是很完善 正在完善中。如果有什么意见和建议,请发送邮件获取 chenxu7601257@qq.com 如果你看了这个帮助文件之后觉得有用的,请发邮件获取,我将把dll文件给你。谢谢。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值