简体   繁体   中英

How can I perform a click action on a button that is present on a child window using pywinauto?

I am able to perform a button click on the parent window but I am not sure how I can perform a click action on the button of a child window.

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

I can see that Yes is a button. but I do not know of a way to perform click() on button yes

I tried the following command which failed with the error message error mesage image

Please let me know how can I perform a click action on the button of a child window.

It returns a list of ButtonWrapper s. So you can choose, for example, the first button and call method .click() for it. This method is silent (mouse cursor is not moved), so another method .click_input() performs more realistic click.

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()

This is a quick fix for your code. More nice way it could look so:

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

If you want to print all possible child_window specifications, use method .dump_tree() for parent window specification:

app.name_of_the_parent_window.dump_tree()

To get wrapper object for the specification use method .wrapper_object() :

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

Then you can type button_wrp. in IDE and see expanded list of available methods of the wrapper. Also it's easy to use dir(button_wrp) , it's a built-in Python function to get all attributes of any Python object (very useful for inspecting objects you're unfamiliar with).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM