开发者

Reading a Dialog Box in Python

开发者 https://www.devze.com 2023-03-21 17:24 出处:网络
I am trying to automate a daily routine I do using Python. Using the win32gui library I h开发者_如何学编程ave been able to put a cursor in a position, click, and sendkeys.During this task, several mes

I am trying to automate a daily routine I do using Python. Using the win32gui library I h开发者_如何学编程ave 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消