import time
from selenium import webdriver
from selenium.webdriver import ActionChains
import win32gui
import win32con
driver=webdriver.Chrome()
#需要访问得网站
driver.get("https://element-plus.gitee.io/zh-CN/component/upload.html")
time.sleep(4)
#获取句柄
el_file=driver.find_element_by_xpath('//*[@]/div/main/div/div/div[1]/div/div[1]/div[1]/div/div[1]/button')
ActionChains(driver).move_to_element(el_file).click().perform()
def uploads(filePath,browser_type="Chrome"):
if browser_type == "Chrome":
title = "打开"
else:
title = "文件上传"
#32770‐ComboBoxEx32 ‐ComboBox ‐Edit
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)
#32770‐Button
button = win32gui.FindWindowEx(dialog,0,"Button",None) #四级
#往文件名编辑框中输入文件路径
#上传操作
win32gui.SendMessage(Edit,win32con.WM_SETTEXT,None,filePath)
#点击打开按钮
win32gui.SendMessage(dialog,win32con.WM_COMMAND,1,button)
#文件路径
filepath="E:\\bb\\1.png"
time.sleep(6)
uploads(filePath=filepath)
print("文件上传成功")
time.sleep(10)
#关闭浏览器
#driver.quit()