1、使用pywin32上传
https://www.jianshu.com/p/fba37cc5d5e2
import win32.lib.win32con as win32con import win32.lib.win32gui_struct as win32gui_struct from time import sleep from selenium import webdriver dr = webdriver.Firefox() dr.get("https://www.baidu.com/") sleep(1) dr.find_elements_by_css_selector(".soutu-btn")[0].click() sleep(2) dr.find_element_by_xpath("//*/div[@class='upload-wrap']").click() # upload.click() # win32gui dialog = win32gui_struct.win32gui.FindWindow('#32770', '文件上传') # 对话框 ComboBoxEx32 = win32gui_struct.win32gui.FindWindowEx(dialog, 0, 'ComboBoxEx32', None) ComboBox = win32gui_struct.win32gui.FindWindowEx(ComboBoxEx32, 0, 'ComboBox', None) Edit = win32gui_struct.win32gui.FindWindowEx(ComboBox, 0, 'Edit', None) # 上面三句依次寻找对象,直到找到输入框Edit对象的句柄 button = win32gui_struct.win32gui.FindWindowEx(dialog, 0, 'Button', None) # 确定按钮Button win32gui_struct.win32gui.SendMessage(Edit, win32con.WM_SETTEXT, None, 'C:\\Users\\cao\\Desktop\\5.jpg') # 往输入框输入绝对地址 win32gui_struct.win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button) # 按button # print (upload.get_attribute('value')) # dr.quit()
2、使用autolt上传
https://zhuanlan.zhihu.com/p/31300170
1、使用autolt获取窗口的title、class、classnameNN
打开按钮的class属性
2、使用编辑器编写脚本 SciTE Script Editor
ControlFocus("文件上传", "", "Edit1")
WinWait("[CLASS:#32770]", "", 10)
ControlSetText("文件上传" ,"", "Edit1", $CmdLine[1])
Sleep(2000)
ControlClick("文件上传", "","Button1");
保存脚本至本地
3、通过工具将脚本转成exe文件 Compile Script to .exe (x64)
4、在程序中调用exe
import os import win32.lib.win32con as win32con import win32.lib.win32gui_struct as win32gui_struct from time import sleep from selenium import webdriver dr = webdriver.Firefox() dr.get("https://www.baidu.com/") sleep(1) dr.find_elements_by_css_selector(".soutu-btn")[0].click() sleep(2) dr.find_element_by_xpath("//*/div[@class='upload-wrap']").click() sleep(3) os.system(r'C:\Users\cao\Desktop\firefox_update.exe "C:\Users\cao\Desktop\5.jpg"') #使用system可以调用exe文件 sleep(3) # driver.quit()