mp252
mp252

Reputation: 475

How to click button in dialog box using PyWinAuto

I have an batch file that I execute that opens a program. A dialog box then appears which I type into it the username and password credentials

The I print the control identifiers and it lists;

SunAwtDialog - 'Login'    (L528, T242, R853, B501)
['SunAwtDialog', 'LoginSunAwtDialog', 'Login']
child_window(title="Login", class_name="SunAwtDialog")

So after reading this post. My understanding was to use window + the button and a click method like so;

dlp.SunAwtDialog['Login'].click()

But this keeps throwing an ElementNotFoundError;

ElementNotFoundError: {'best_match': 'SunAwtDialog', 'top_level_only': False, 'parent': <win32_element_info.HwndElementInfo - 'Login', SunAwtDialog, 2164976>, 'backend': 'win32'}

Below is the full snippet of code;

from pywinauto import application
import time
app = application.Application()
app.start(r"C:\\WINDOWS\system32\cmd.exe", wait_for_idle=False)
dlg = app.top_window()
dlg.type_keys('D:{ENTER}')
dlg.type_keys('cd{SPACE}Software\\client{ENTER}')
dlg.type_keys('run_client.bat{ENTER}')
time.sleep(10)
new_app = application.Application().connect(title="iManager")
dlp = new_app.top_window()
#type username + password
dlp.type_keys('user')
dlp.type_keys('{TAB}')
dlp.type_keys('pass')
#print control identifiers
dlp.print_control_identifiers()
#click login[![enter image description here][1]][1]
dlp.SunAwtDialog['Login'].click()

You can see on the image below the "Login" button I want to be able to click. There is also another button next to the "Server" option, but it is not in my control identifiers

Dialog box

Upvotes: 3

Views: 6311

Answers (2)

SURAJ ABDAR
SURAJ ABDAR

Reputation: 1

The good solution for this is pressing the "Enter" if there is only an "OK" button on that Dialog/Message/Alert/Information box. For this you can use:

send_keys("{ENTER}")

If that doesn't work correctly, you can use the sleep command to wait for 1-2 seconds and then press enter, like so:

time.sleep(2)
send_keys("{Enter}")

For this, you have to import two libraries:

from pywinauto.keyboard import send_keys
import time

Upvotes: 0

qbbq
qbbq

Reputation: 367

A solution that I use for this is using send_keys. Try this:

from pywinauto.keyboard import send_keys
send_keys("{VK_MENU down}" "l" "{VK_MENU up}")

Upvotes: 1

Related Questions