Python-UIAutomation-for-Windows icon indicating copy to clipboard operation
Python-UIAutomation-for-Windows copied to clipboard

如何快速的查找元素?

Open shidecheng opened this issue 3 years ago • 2 comments
trafficstars

作者你好,我是刚刚接触这个库,觉得非常好用;我想问一下,如何快速的查找控件。 我通过光标获取到了元素控件,然后我把元素控件里面的Name,automationId, ClassName, ControlType这几个属性存储在了本地文件。我想实现快速的根据存储信息查找元素。我目前是通过这种方式找的,但是感觉不是很快,下面是部分代码截图: tips: 是通过读取本地存储的元素信息进行查找的。 with open(self._locators_path, 'r') as f: locators_data = json.loads(f.read())[name] control = auto.GetRootControl() def compare(locator): def find(child, depth): if child.Name == locator['Name'] and child.ClassName == locator['ClassName'] and child.ControlTypeName == locator['ControlType'] and child.AutomationId == locator['AutomationId']: print(child.Name) return True else: return False return find l = len(locators_data) for index in range(l): if index == l - 1: control = auto.FindControl(control, compare(locators_data[index]), depth) if control: return WindowsElement(control) else: control = auto.FindControl(control, compare(locators_data[index]), 1) return None

shidecheng avatar Mar 18 '22 12:03 shidecheng

可以把控件树中到达目标控件的路径中的各个控件的属性保存下来,然后Control.Control一层一层搜索,不知道会不会更快,但是感觉稳定一点

L-Yucheng avatar Oct 19 '22 01:10 L-Yucheng

Name,automationId, ClassName, ControlType,这几个属性通常也定位不了,经常都空值,我都是一层一层定位,自己有把pywinauto的draw_outline()函数移植过来,边写代码边调试,确实费时间,要是能自动生成定位元素的路径那该多好,想想也不是什么难事,有时间打算试一下

qq463505965 avatar Jun 23 '23 10:06 qq463505965