python+autoit 实现网页文件上传
2020-03-18 本文已影响0人
苏未_
参考:https://blog.csdn.net/justheretobe/article/details/50939021
webdriver无法对windows的图形界面进行操作,所以借助AutoIt来实现文件上传。
AutoIt下载: https://www.autoitscript.com/site
安装后打开Au3Info_x64.exe
![](https://img.haomeiwen.com/i4237968/4fbc0ab14faf6271.png)
打开工具后,用鼠标讲Finder Tool图标拖动到要识别的控件上
1.获取文本框的控件信息:
![](https://img.haomeiwen.com/i4237968/31793be74dcf6a47.png)
2.获取“打开”按钮的控件信息:
![](https://img.haomeiwen.com/i4237968/e37c5237823db359.png)
编写AutoIt脚本,实现文件上传
1.打开SciTE.exe
![](https://img.haomeiwen.com/i4237968/bf74713e92b53c46.png)
2.代码
;ControlFocus("title", "text", controlID) Edit1=Edit instance 1
ControlFocus("打开", "","Edit1")
;Wait 10 seconds for the Upload window to appear
WinWait("[CLASS:#32770]", "",10)
;Set the File name thext on the Edit field
ControlSetText("打开", "", "Edit1", "D:\test.txt")
Sleep(2000)
;Click on the Open button
ControlClick("打开", "", "Button1");
3.将文件保存upfile.au3
![](https://img.haomeiwen.com/i4237968/0a76c562f7e19e94.png)
4.使用Aut2exe_x64.exe将上面的脚本编译成exe文件供python使用
![](https://img.haomeiwen.com/i4237968/5ea5688bc73dad38.png)
![](https://img.haomeiwen.com/i4237968/281f7aab614d486c.png)
使用python脚本调用AutoIt脚本
#点击上传,打开上传文件窗口
driver.find_element_by_xpath()
#使用autoit脚本自动上传文件
#需要导入python的os库文件: import os
os.system("D:/upfile.exe")