一开始视频学习的代码,打开windows窗口后,没有反应
time.sleep(1)
import win32gui
import win32con
dialog = win32gui.FindWindow("#32770", "打开")
comboxex32 = win32gui.FindWindowEx(dialog, 0,
combox = win32gui.FindWindowEx(comboxex32, 0, "ComboBox", None)
edit = win32gui.FindWindowEx(combox, 0, "Edit", None)
button = win32gui.FindWindowEx(dialog, 0, "Button", "打开(&0)")
win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, r'C:\Users\sam\Pictures\test.png)
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
time.sleep(2)
后面查到的代码,这个管用,但是不知道为什么
(参考博客原址https://www.cnblogs.com/xiamaojjie/p/12151769.html)
time.sleep(1)
import win32gui
import win32con
def upload_file(file_path):
'''
:param file_path:上传文件的路径
:return:
'''
dialog = win32gui.FindWindow("#32770", "打开") # # win32gui.FindWindow("类名","标题")
comboxex32 = win32gui.FindWindowEx(dialog, 0, "ComboBoxEx32", None)# # win32gui.FindWindowEx(父窗体,子窗口,"类名",窗口名)
combox = win32gui.FindWindowEx(comboxex32, 0, "ComboBox", None)# 0,表示从第一个子窗体开始搜索
edit = win32gui.FindWindowEx(combox, 0, "Edit", None)# # 找到文件名后面的文本框
button = win32gui.FindWindowEx(dialog, 0, "Button", "打开(&0)")
win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_path)
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
upload_file(r'C:\Users\saml\Pictures\test.png')