首页 > 代码库 > [UIA]UIA获取或设置文本框的内容
[UIA]UIA获取或设置文本框的内容
问题:获取Text控件的内容
场景:Win+R启动运行框,需要设置或获取Edit控件中的内容
解决:
1.使用.NETFramework
UIA中有个TextPattern,里面有TextPatternRange,即可得到Text的内容
代码如下:
var desktop = AutomationElement.RootElement; var condition_Name = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window); // 定义我们的查找条件,名字是test var condition_Class = new PropertyCondition(AutomationElement.NameProperty, "运行"); // 定义我们的查找条件,名字是test var window = desktop.FindFirst(TreeScope.Subtree, new AndCondition(condition_Class, condition_Name)); // 在桌面的子控件中查找第一个符合条件的窗体。 var condition_Edit = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit); var textedit = window.FindFirst(TreeScope.Subtree, condition_Edit); Console.WriteLine(textedit.Current.ProcessId); Console.WriteLine(textedit.Current.ClassName); Console.WriteLine(textedit.Current.NativeWindowHandle); var clickPattern = (TextPattern)textedit.GetCurrentPattern(TextPattern.Pattern); Console.WriteLine(clickPattern.DocumentRange.GetText(100)); Console.WriteLine(clickPattern.DocumentRange.GetText(100));
按道理,com接口应该与.NET Framework一致,即先获取TextPattern,然后获取TextPatternRange,获取Text,但是通过com接口获取到的TextPattern为Null
com的过程应该是:获取ValuePattern,使用SetValue或CurrentValue,设置或获取值
代码如下:
#获取文本框内容 desktop = AutomationElement.GetRootElement() controlTypeProperty = PropertyCondition(UIAPropertyIds.UIA_ControlTypePropertyId, UIAControlTypeIds.UIA_WindowControlTypeId) nameProperty = PropertyCondition(UIAPropertyIds.UIA_NamePropertyId, u"运行") window = desktop.FindFirst(TreeScope.TreeScope_Descendants, AndCondition(controlTypeProperty, nameProperty)) print window.GetWindowPattern() subControlTypeProperty = PropertyCondition(UIAPropertyIds.UIA_ControlTypePropertyId, UIAControlTypeIds.UIA_EditControlTypeId) textedit = window.FindFirst(TreeScope.TreeScope_Descendants, subControlTypeProperty) print textedit.element print textedit.Current.GetProcessId() print textedit.Current.GetClassName() print hex(textedit.Current.GetNativeWindowHandle()) buttonProperty = PropertyCondition(UIAPropertyIds.UIA_NamePropertyId, u"确定") button = window.FindFirst(TreeScope.TreeScope_Descendants, buttonProperty) print textedit.GetValuePattern().CurrentValue
[UIA]UIA获取或设置文本框的内容
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。