我正在尝试使我使用 Python 完成的日常工作自动化。使用 win32gui 库,我能够将光标放在某个位置、单击和发送键。在此任务期间,会弹出几个消息/对话框,询问某些问题是或否。我希望能够阅读消息框中的问题,但不知道我将如何处理它。谢谢
1 回答
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.