Pywinauto how do I get the list of returned elements

萝らか妹 提交于 2019-12-11 07:43:56

问题


I'm using a WPF application that has custom stack panel, which is basically a list. The item in the list is exactly the same so I'm not able to select a specific text to uniquely identify the elements. And some other values such as time are dynamic.

Is there a way for me to get the list of elements returned. I know it's possible because the error was thrown, ElementAmbiguousError state the count.

If I could do that, then from that list I can use the index and validate what I need.


回答1:


You can add found_index=0 or other index to the window specification object. This is the first way to disambiguate the search.

Also there are methods .children() and .descendants() with additional params like control_type or title (as I remember title should work), but some window specification params are not supported in these methods.




回答2:


elements = pywinauto.findwindows.find_elements(<args>)
len(elements)

<args> will need to contain more detailed information than it does when using something like Application().connect(process=<pid>).window().child_window(<args>) because it doesn't have information in the chain about the window in which you're searching. See the documentation on this method for more information.

Also note there is a warning that using this method directly is not recommended as it is a low level API.

Another solution (already provided by vasily) is to parse the error that comes back when multiple elements are found to isolate the number found.

Unless the pywinauto devs decide to provide a higher-level API for accessing the number of elements found, you'll have to weigh the risk of the low-level API changing with the risk of parsing an error message which could also change. I have this problem too and have decided to use find_elements()



来源:https://stackoverflow.com/questions/46432544/pywinauto-how-do-i-get-the-list-of-returned-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!