-
Notifications
You must be signed in to change notification settings - Fork 672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable finding of undisplayed menu items #827
base: 0_7_x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -938,9 +938,27 @@ def __init__(self, elem): | |
super(MenuItemWrapper, self).__init__(elem) | ||
|
||
# ----------------------------------------------------------- | ||
_cache_items = {} | ||
def items(self): | ||
"""Find all items of the menu item""" | ||
return self.children(control_type="MenuItem") | ||
items = self.children(control_type="MenuItem") | ||
|
||
# If a menu is not displayed then the UIA controls (children of self) | ||
# will not yet exist. We can bring them into existence by clicking the | ||
# menu (self). The controls can also (mysteriously) lose all their | ||
# element_info. So once we see them bv clicking the menu we cache them | ||
# for subsequent use. | ||
if not items: | ||
if self._cache_items.get(self.element_info.automation_id, None): | ||
items = self._cache_items[self.element_info.automation_id].copy() | ||
else: | ||
self.click_input() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what you mean by "invasive" or what alternative there is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Invasive" means that method Yes, some apps have better design of menu structure so the items are invisible but accessible. In your case a dynamic action is required to list the items. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per original note above: Part of me thinks the earlier solution I suggest of an optional argument click_to_find_children=False is impact neutral on existing functionality and does nothing special. But I'm loving the easy wrapper override facility that makes experimenting with solutions easy. |
||
if self.children(): | ||
me = self.children()[0] | ||
items = me.children(control_type="MenuItem") | ||
self._cache_items[self.element_info.automation_id] = items.copy() | ||
|
||
return items | ||
|
||
# ----------------------------------------------------------- | ||
def select(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't rely on
automation_id
property as a key of dictionary because it's possible to have child items without such property. I can't provide an example right now because of being too busy, but it's worth trying different apps with this approach to test everything works the same way or better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, time flies. I too am only looking at this again now. If
automation_id
is not reliable we could just check that it is available and if it is, then cache the item.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never say "Never!". :) Yeah, that may be better, but why not use
self.element_info.name
? Maybe it's a specific menu implementation. Is it Qt5 app?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope a VCL (Borland) app. I have of course since discovered how neat pywinauto's implementation of metaclasses is such that I simply define a new class derived from this one, and override items, and that is respected and used by pywinauto (the metaclass updates a dict mapping of controls to wrappers). Which means I'm actually free to experiment willy-nilly on broader impacts. Part of me thinks the earlier solution I suggest of an optional argument click_to_find_children=False is impact neutral on existing functionality and does nothing special.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the details! I will think what we can do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out rather that
automation_id
which indeed proved unreliably available, the only key I could find for a caching dict iselement_info.element
which seems to be a unique pointer (which Python permits as a dict key). Problem is nothing else I could find anywhere underelement_info
provides a unique handle on the element (includename
which is blank on most of our controls).