在做web端UI层自动化的时候,常会碰到文件上传的操作。
代码
# win32gui
dialog = win32gui.FindWindow('#32770', u'打开') # 对话框
ComboBoxEx32 = win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None)
ComboBox = win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None)
Edit = win32gui.FindWindowEx(ComboBox, 0, 'Edit', None) # 上面三句依次寻找对象,直到找到输入框Edit对象的句柄
button = win32gui.FindWindowEx(dialog, 0, 'Button', None) # 确定按钮Button
win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, r'C:\Users\Desktop\Guide.xlsx') # 往输入框输入绝对地址
sleep(2)
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) # 按button
sleep(2)
textAlert = driver.find_element_by_xpath("//div[@class='ui small modal transition visible active']/div[2]").text
if textAlert == "上传成功":
print("上传成功!")
else:
print("上传失败。")
sleep(2)
driver.find_element_by_xpath("//div[@class='ui small modal transition visible active']/div[3]/button").click()