使用 pywinauto 自动安装软件时出错

Error during automated software install using pywinauto

搜索后找不到好的回复:

我正在尝试使用 pywinauto 自动化公司软件。我尽量使用 app.print_control_identifiers() 。这是上帝派来的!感谢 Youtube 上的人们和 Vasily 的 posts。请参阅下面的代码。

在安装过程中,它想要安装一个 MS VC++ 可再发行组件,我可以处理。

下一期告诉我 Adob​​e Flash Player 的版本(我知道对吧?)不兼容。我已经使用 Inspect 来识别我需要单击以继续的 OK 按钮。这是代码(针对 public 消耗进行了清理):

from pywinauto.application import Application
import time

app = Application(backend="uia").start("C:\Users\me\program.exe")
time.sleep(5)
#This addresses the need to install Microsoft C++ Redistributable
dlg = app['program - InstallShield Wizard']
dlg.Install.click()
time.sleep(5)

#This is to get past an install failure and to move on
dlg.Yes.click()
time.sleep(10)

#This is a dialog box saying that some component is incompatible and this is where I am stuck
# And the window title now just says 'program' vs. 'program - InstallShield Wizard'
new_dlg = app['program']
new_dlg.OK.click()

这是失败信息:

C:\Python37\python.exe C:/Users/me/PycharmProjects/myProject/pywinauto_install.py
Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\pywinauto\application.py", line 258, in __resolve_control
    criteria)
  File "C:\Python37\lib\site-packages\pywinauto\timings.py", line 458, in wait_until_passes
    raise err
pywinauto.timings.TimeoutError

在处理上述异常的过程中,又发生了一个异常:

Traceback (most recent call last):

  File "C:/Users/me/PycharmProjects/myProject/pywinauto_install.py", line 23, in <module>
    new_dlg.OK.click()

  File "C:\Python37\lib\site-packages\pywinauto\application.py", line 379, in __getattribute__
    ctrls = self.__resolve_control(self.criteria)

  File "C:\Python37\lib\site-packages\pywinauto\application.py", line 261, in __resolve_control
    raise e.original_exception

  File "C:\Python37\lib\site-packages\pywinauto\timings.py", line 436, in wait_until_passes
    func_val = func(*args, **kwargs)

  File "C:\Python37\lib\site-packages\pywinauto\application.py", line 203, in __get_ctrl
    dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))

  File "C:\Python37\lib\site-packages\pywinauto\findwindows.py", line 87, in find_element
    raise ElementNotFoundError(kwargs)

pywinauto.findwindows.ElementNotFoundError: {'best_match': 'program', 'backend': 'uia', 'process': 22184}

Process finished with exit code 1

我是根据我看到的一个SOpost创建的new_dlg,所以如果有错,请拍我的手。我不是受过训练的开发人员。 谢谢

要检查顶级对话框的文本 windows 打印:

print([w.window_text() for w in app.windows()])

然后只需将正确的一个复制到您的代码中作为字典键而不是 app['program']。更灵活的方式看起来是这样的:

new_dlg = app.window(title_re="Program - .*")