PywinAuto瞄准兄弟元素

时间:2018-03-16 01:51:19

标签: python pywinauto

嘿,我有一个非常糟糕的标签元素,我需要从中获取文本。它被我不想要的其他元素所包围。

我可以轻松地定位我想要的元素的兄弟。

我如何定位" Next Sibling"的?

dlg4.window(标题=' WindowTitleThing',CLASS_NAME ='文本&#39)。draw_outline()

我可以针对第一个孩子?

使用UIA后端。谢谢!

找到了这个,但无法让它发挥作用:

# Monkey patch for pywinauto that adds a first_only parameter to the window function 
# for the UIA backend, in order to work around slow FindAll calls (win10 bug?)
# First copy paste this code on your REPL, then do:
# d1 = pywinauto.Desktop("uia")
# d1.window(first_only=True, title="Calculator").window_text()
# Currently only title is supported, but its easy to implement all the others as well,
# most importantly title_re

def first_child(self):
    print("Getting first child")
    child = pywinauto.uia_defines.IUIA().iuia.RawViewWalker.GetFirstChildElement(self._element)
    if child:
        return pywinauto.uia_element_info.UIAElementInfo(child)
    else:
        return None

def next_sibling(self):
    print("Getting sibling")
    sibling = pywinauto.uia_defines.IUIA().iuia.RawViewWalker.GetNextSiblingElement(self._element)
    if sibling:
        return pywinauto.uia_element_info.UIAElementInfo(sibling)
    else:
        return None

def find_first_element(first_only=None,
                       title=None,
                       title_re=None,
                       top_level_only=True,
                       backend=None
                       ):
    if backend is None:
        backend = pywinauto.backend.registry.active_backend.name
    backend_obj = pywinauto.backend.registry.backends[backend]
    if not top_level_only:
        raise NotImplementedError  # or can we actually accept this?
    rootElement = backend_obj.element_info_class()
    element = None
    child = rootElement.first_child()
    while child is not None:
        print(child.name + " ?= " + title)
        if child.name == title: 
            # TODO all the other conditions..
            # class_name(_re)
            # title_re
            # process
            # visible / enabled / handle / predicate_func / active_only / control_id / control_type / auto_id / framework_id
            break
        child = child.next_sibling()
    return child

def new_find_element(**kwargs):
    if 'first_only' in kwargs and kwargs['first_only'] is True:
        print("Using patched function to get only the first match")
        el = pywinauto.findwindows.find_first_element(**kwargs)
        if el is None:
            raise pywinauto.findwindows.ElementNotFoundError(kwargs)
        else:
            return el
    else:
        print("Using original function")
        return pywinauto.findwindows.original_find_element(**kwargs)

import pywinauto
pywinauto.uia_element_info.UIAElementInfo.first_child = first_child
pywinauto.uia_element_info.UIAElementInfo.next_sibling = next_sibling
pywinauto.findwindows.find_first_element = find_first_element
pywinauto.findwindows.original_find_element = pywinauto.findwindows.find_element
pywinauto.findwindows.find_element = new_find_element

>>> first_child(dlg4)
Getting first child
Using original function
Using original function
Using original function
Using original function
Using original function
Using original function
Using original function
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in first_child
ctypes.ArgumentError: argument 1: <class 'pywinauto.findbestmatch.MatchError'>: Could not find '_element' in 'dict_keys(

0 个答案:

没有答案