得到pywinauto.uia_defines.NoPatternInterfaceError是否正常

问题描述 投票:0回答:1

我编写了一个程序来自动测试我没有使用 pywinauto 创建的应用程序,但我收到此错误:pywinauto.uia_defines.NoPatternInterfaceError。当我尝试这个时它出现了:

    app.connect(path=exe_path)
    main_dlg = app.top_window()
    main_dlg.wait('visible')
    truc = main_dlg.child_window(title="Users", control_type="Button")
    truc.click() # <- this is where the problem arised

这是代码(以便您理解上下文):

from pywinauto.application import Application

*
* This is irrevelent
*

try:
    print("start")
    app = Application(backend="uia").start(exe_path)

    app.connect(path=exe_path)
    main_dlg = app.top_window()
    print("Is there a window : "+ str(main_dlg.exists()))
    main_dlg.wait('visible')


    # Here we can start to work

    # This let us see what the API see in the selected window
    main_dlg.print_control_identifiers()
    print(str(main_dlg.actions))

    truc = main_dlg.child_window(title="Users", control_type="Button")
    print(type(truc))
    print(str(truc))
    truc.print_control_identifiers()
    truc.click()

except Exception as e:
    print("PROBLEM")
    print("--------------------------------------------------------------")
    raise(e)
finally:
    print("Finally")

这是它打印的内容:

C:\Users\vboxuser\Documents\GST_tests\automation.py:3: SyntaxWarning: invalid escape sequence '\P'
  exe_path = "xxx"
start
Is there a window : True
Control Identifiers:

Dialog - 'xxx'    (L-8, T-8, R1928, B868)
['xxxDialog', 'Dialog', 'xxx']
child_window(title="xxx", control_type="Window")
   |
   | TitleBar - ''    (L16, T-5, R1920, B23)
   | ['TitleBar']
   |    |
   |    | Menu - 'Système'    (L0, T0, R22, B22)
   |    | ['SystèmeMenu', 'Système', 'Menu', 'Système0', 'Système1']
   |    | child_window(title="Système", auto_id="MenuBar", control_type="MenuBar")
   |    |    |
   |    |    | MenuItem - 'Système'    (L0, T0, R22, B22)
   |    |    | ['Système2', 'MenuItem', 'SystèmeMenuItem']
   |    |    | child_window(title="Système", control_type="MenuItem")
   |    |
   |    | Button - 'Réduire'    (L1779, T8, R1826, B22)
   |    | ['Button', 'Réduire', 'RéduireButton', 'Button0', 'Button1']
   |    | child_window(title="Réduire", control_type="Button")
   |    |
   |    | Button - 'Restaurer'    (L1826, T8, R1872, B22)
   |    | ['Button2', 'RestaurerButton', 'Restaurer']
   |    | child_window(title="Restaurer", control_type="Button")
   |    |
   |    | Button - 'Fermer'    (L1872, T8, R1928, B22)
   |    | ['Button3', 'FermerButton', 'Fermer']
   |    | child_window(title="Fermer", control_type="Button")
   |
   | Image - ''    (L16, T39, R408, B103)
   | ['Image']
   |
   | Button - 'Logon'    (L0, T120, R100, B170)
   | ['Logon', 'Button4', 'LogonButton', 'Logon0', 'Logon1']
   | child_window(title="Logon", control_type="Button")
   |    |
   |    | Static - 'Logon'    (L33, T137, R67, B152)
   |    | ['Logon2', 'Static', 'LogonStatic', 'Static0', 'Static1']
   |    | child_window(title="Logon", control_type="Text")
   |
   | Button - 'Hardware'    (L0, T170, R100, B220)
   | ['Hardware', 'Button5', 'HardwareButton', 'Hardware0', 'Hardware1']
   | child_window(title="Hardware", control_type="Button")
   |    |
   |    | Static - 'Hardware'    (L24, T187, R76, B202)
   |    | ['Hardware2', 'Static2', 'HardwareStatic']
   |    | child_window(title="Hardware", control_type="Text")
   |
   | Button - 'SNTP'    (L0, T220, R100, B270)
   | ['Button6', 'SNTP', 'SNTPButton', 'SNTP0', 'SNTP1']
   | child_window(title="SNTP", control_type="Button")
   |    |
   |    | Static - 'SNTP'    (L36, T237, R64, B252)
   |    | ['SNTPStatic', 'Static3', 'SNTP2']
   |    | child_window(title="SNTP", control_type="Text")
   |
   | Button - 'Network'    (L0, T270, R100, B320)
   | ['Button7', 'NetworkButton', 'Network', 'Network0', 'Network1']
   | child_window(title="Network", control_type="Button")
   |    |
   |    | Static - 'Network'    (L27, T287, R73, B302)
   |    | ['Static4', 'NetworkStatic', 'Network2']
   |    | child_window(title="Network", control_type="Text")
   |
   | Button - 'Users'    (L0, T320, R100, B370)
   | ['UsersButton', 'Button8', 'Users', 'Users0', 'Users1']
   | child_window(title="Users", control_type="Button")
   | *
   | *
   | *
   | * Not useful info that i cut
   | *
   | *
   | *
   | Button - 'Firmware'    (L0, T570, R100, B620)
   | ['Button13', 'Firmware', 'FirmwareButton', 'Firmware0', 'Firmware1']
   | child_window(title="Firmware", control_type="Button")
   |    |
   |    | Static - 'Firmware'    (L26, T587, R74, B602)
   |    | ['Static10', 'FirmwareStatic', 'Firmware2']
   |    | child_window(title="Firmware", control_type="Text")
<pywinauto.actionlogger._StandardLogger object at 0x000001C3EC87B290>
<class 'pywinauto.application.WindowSpecification'>
<pywinauto.application.WindowSpecification object at 0x000001C3EDAC3410>
Control Identifiers:

Button - 'Users'    (L0, T320, R100, B370)
['UsersButton', 'Button', 'Users', 'Users0', 'Users1']
child_window(title="Users", control_type="Button")
   |
   | Static - 'Users'    (L36, T337, R64, B352)
   | ['Static', 'Users2', 'UsersStatic']
   | child_window(title="Users", control_type="Text")
PROBLEM
--------------------------------------------------------------
Finally
Traceback (most recent call last):
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\uia_defines.py", line 234, in get_elem_interface
    iface = cur_ptrn.QueryInterface(cls_name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\comtypes\__init__.py", line 828, in QueryInterface
    self.__com_QueryInterface(byref(iid), byref(p))
ValueError: NULL COM pointer access

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\controls\uia_controls.py", line 121, in click
    self.invoke()
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\controls\uiawrapper.py", line 513, in invoke
    self.iface_invoke.Invoke()
    ^^^^^^^^^^^^^^^^^
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\controls\uiawrapper.py", line 132, in __get__
    value = self.fget(obj)
            ^^^^^^^^^^^^^^
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\controls\uiawrapper.py", line 231, in iface_invoke
    return uia_defs.get_elem_interface(elem, "Invoke")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\uia_defines.py", line 236, in get_elem_interface
    raise NoPatternInterfaceError()
pywinauto.uia_defines.NoPatternInterfaceError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\uia_defines.py", line 234, in get_elem_interface
    iface = cur_ptrn.QueryInterface(cls_name)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\comtypes\__init__.py", line 828, in QueryInterface
    self.__com_QueryInterface(byref(iid), byref(p))
ValueError: NULL COM pointer access

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\vboxuser\Documents\GST_tests\automation.py", line 31, in <module>
    raise(e)
  File "C:\Users\vboxuser\Documents\GST_tests\automation.py", line 26, in <module>
    truc.click()
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\controls\uia_controls.py", line 123, in click
    self.select()
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\controls\uiawrapper.py", line 605, in select
    self.iface_selection_item.Select()
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\controls\uiawrapper.py", line 132, in __get__
    value = self.fget(obj)
            ^^^^^^^^^^^^^^
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\controls\uiawrapper.py", line 224, in iface_selection_item
    return uia_defs.get_elem_interface(elem, "SelectionItem")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\vboxuser\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pywinauto\uia_defines.py", line 236, in get_elem_interface
    raise NoPatternInterfaceError()
pywinauto.uia_defines.NoPatternInterfaceError

根据我的理解以及我在文档和示例中看到的内容,用户按钮应该是一个可点击的元素。我做错什么了吗?

另外,我发现这篇文章:github问题链接似乎也有类似的问题。我也尝试使用 win32 后端而不是 uia,但我得到了与 ripdog 相同的结果。

最后可以总结为:是我的错吗?是否有其他方法来单击按钮元素?

python ui-automation pywinauto
1个回答
0
投票

不幸的是,并非每个应用程序都在应用程序端正确实现了单击处理程序和其他处理程序。所以这是一种常态。 :( 但是方法

.click_input()
.type_keys(...)
和类似方法应该始终有效,因为它们使用本机鼠标和键盘事件。它们只需要活动桌面:这意味着 PC 未锁定或 RDP 窗口未最小化且未断开连接。关于 RDP您可以阅读远程执行指南以获取正确的设置和其他解决方法

© www.soinside.com 2019 - 2024. All rights reserved.