sss
sss

Reputation: 11

Reading a Dialog Box in Python

I am trying to automate a daily routine I do using Python. Using the win32gui library I have been able to put a cursor in a position, click, and sendkeys. During this task, several message/dialog boxes pop up, asking yes or no, to certain questions. I would like to be able to read the question in the message box , but have no idea how I will approach it. Thanks

Upvotes: 1

Views: 2041

Answers (1)

robots.jpg
robots.jpg

Reputation: 5161

Just to give you one possibility, you may be able to copy the full contents of a dialog box to the clipboard by sending a Ctrl-C with sendkeys while focus is on the dialog. The contents of the clipboard will generally look something like this:

[Window Title]
Rename

[Content]
If you change a file name extension, the file might become unusable.

Are you sure you want to change it?

[Yes] [No]

win32clipboard from the pywin32 library allows you retrieve the contents of the clipboard so you can parse the text and respond, or do whatever else you need to do with it:

import win32con
import win32clipboard

try:
    win32clipboard.OpenClipboard()
    text = win32clipboard.GetClipboardData(win32con.CF_TEXT)
    print(text)
except TypeError:
    print('Error: No text on the clipboard!')
finally:
    win32clipboard.CloseClipboard()

Unfortunately, there are some dialogs that can't be copied to the clipboard like this. I couldn't tell you the reason, but the Delete Confirmation dialog in Windows 7 is one of them.

Upvotes: 2

Related Questions