转https://blog.csdn.net/qq_32897143/article/details/82014603
https://www.jb51.net/article/92678.htm
安装win32系列
pip install pypiwin32
from selenium import webdriver
import os
# 1.input标签
#对于input标签可以直接send_keys的
# b = webdriver.Chrome()
# b.get("http://www.sahitest.com/demo/php/fileUpload.htm")
# upload_ele = b.find_element_by_id('file')
# upload_ele.send_keys(r'C:\Users\shitou\eclipse-workspace\Web_GUI_Test_Python_Selenium\project\business\test.py') # send_keys
# print(upload_ele.get_attribute('value')
# 2.非input型上传
# 2.1autoIT (参考https://www.cnblogs.com/caoj/p/7815835.html)
#autoit程序
"""
;first make sure the number of arguments passed into the scripts is more than 1
If $CmdLine[0]<2 Then Exit EndIf ;if parmas num <2 ,then break
;$CmdLine[0] ;参数的数量
;$CmdLine[1] ;第一个参数 (脚本名称后面)
;$CmdLine[2] ;第二个参数
;都是从cmd传入参数
handleUpload($CmdLine[1],$CmdLine[2])
;定义上传函数,有两个参数,第一个是浏览器名字,第二参数是文件路径
Func handleUpload($browser, $uploadfile)
Dim $title ;定义一个title变量
;根据弹窗的title来判断是什么浏览器
If $browser="ie" Then ; 代表IE浏览器
$title="选择要加载的文件"
ElseIf $browser="chrome" Then ; 代表谷歌浏览器
$title="打开"
ElseIf $browser="firefox" Then ; 代表火狐浏览器
$title="文件上传"
EndIf
if WinWait($title,"",10) Then ;等待弹窗出现,最大等待时间是10秒
WinActivate($title) ;找到弹出窗口之后,激活当前窗口
ControlSetText($title,"","Edit1",$uploadfile) ;把文件路径放入输入框,此”Edit1“是用FinderTool获取到的
ControlClick($title,"","Button1") ;点击保存或者打开或者上传按钮,此“Button1”使用FinderTool获取到的
Else
Return False
EndIf
EndFunc
"""
b = webdriver.Chrome()
b.get("http://www.sahitest.com/demo/php/fileUpload.htm")
upload_ele = b.find_element_by_id('file')
upload_ele.click()
#执行程序
os.system("upload.exe %s %s"%("chrome", r"C:\Users\shitou\eclipse-workspace\Web_GUI_Test_Python_Selenium\project\business\test.py"))
dr.quit()
## 2.2 win32gui
import win32con
import win32gui
# # 上传
def open_file_dialog(file_path):
dialog = win32gui.FindWindow("#32770", u"打开")
if dialog == 0:
return False
assert dialog != 0
button = win32gui.GetDlgItem(dialog, int("00000001", 16))
assert button != 0
edit = win32gui.GetDlgItem(dialog, int("0000047C", 16))
assert edit != 0
win32gui.SendMessage(edit, win32con.WM_SETTEXT, None, file_path)
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
return True
open_file_dialog(r"C:\Python36\IEDriverServer.exe")
# # 下载
def open_file_dialog():
dialog = win32gui.FindWindow("#32770", u"另存为")
if dialog == 0:
return False
assert dialog != 0
button = win32gui.FindWindowEx(dialog,0,"Button",None)
assert button != 0
win32gui.SendMessage(dialog, win32con.WM_COMMAND, 1, button)
return True
open_file_dialog()