【问题标题】:How can I perform a click action on a button that is present on a child window using pywinauto?如何使用 pywinauto 对子窗口上存在的按钮执行单击操作?
【发布时间】:2019-12-11 18:31:09
【问题描述】:

我能够在父窗口上执行按钮点击,但我不确定如何在子窗口的按钮上执行点击操作。

app = Application().connect(path=newPath) 
app.name_of_the_parent_window.draw_outline()
app.name_of_the_parent_window.print_control_identifiers()
app.name_of_the_parent_window.Button6.click()  #this click will open a new window with yes & no buttons.I would like to click on yes button but I am unable to do that

print(app.windows()[0].children(title_re=".*would you like to reset.*", class_name="Button")) #this prints the information about buttons

image of the result of the above print command

我可以看到 Yes 是一个按钮。但我不知道在按钮上执行 click() 的方法是

我尝试了以下命令,但失败并显示错误消息 error mesage image

请告诉我如何在子窗口的按钮上执行点击操作。

【问题讨论】:

    标签: python-3.x button click pywinauto


    【解决方案1】:

    它返回ButtonWrappers 的列表。因此,您可以选择例如第一个按钮并为其调用方法.click()。这个方法是静默的(鼠标光标没有移动),所以另一个方法.click_input()执行更真实的点击。

    app = Application().connect(path=newPath) 
    app.name_of_the_parent_window.draw_outline()
    app.name_of_the_parent_window.print_control_identifiers()
    app.name_of_the_parent_window.Button6.click()  #this click will open a new window with yes & no buttons.I would like to click on yes button but I am unable to do that
    
    buttons = app.windows()[0].children(title_re=".*would you like to reset.*", class_name="Button")
    print(buttons) #this prints the information about buttons
    buttons[0].click() # or buttons[0].click_input()
    

    这是对您的代码的快速修复。看起来更漂亮的方式:

    app.name_of_the_parent_window.child_window(title="&Yes", class_name="Button").click()
    

    如果要打印所有可能的child_window 规范,请使用.dump_tree() 方法作为父窗口规范:

    app.name_of_the_parent_window.dump_tree()
    

    获取规范使用方法.wrapper_object()的包装对象:

    button_wrp = app.name_of_the_parent_window.child_window(title="&Yes", class_name="Button").wrapper_object()
    

    然后您可以在 IDE 中键入 button_wrp. 并查看包装器可用方法的扩展列表。 dir(button_wrp) 也很容易使用,它是一个内置的 Python 函数,可以获取任何 Python 对象的所有属性(对于检查你不熟悉的对象非常有用)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 2013-04-04
      相关资源
      最近更新 更多