import win32gui
import win32con
import random
def upload(filePath, browser_type="chrome"):
"""
上传文件前,上传窗口必须打开
:param filePath:
:param browser_type:
:return:
"""
if browser_type == "chrome": #更改1,浏览器
title = "打开"
else:
title = ""
# 找元素
# 一级窗口"#32770","打开"
dialog = win32gui.FindWindow("#32770", title)
#
ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, "ComboBoxEx32", None) # 二级
comboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, "ComboBox", None) # 三级
# 编辑按钮
edit = win32gui.FindWindowEx(comboBox, 0, 'Edit', None) # 四级
# 打开按钮
button = win32gui.FindWindowEx(dialog, 0, 'Button', "打开(&O)") # 二级
# 往编辑当中,输入文件路径 。
win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, filePath) # 发送文件路径
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) # 点击打开按钮
upload("D:\\chromedriver.log") #更改2 文件存储路径要给绝对路径