Python + Seleium + Autolt realize automatic file upload

1. Download Autolt

What I downloaded here is zip decompression can be used, in fact, there are 3 files needed, but in this folder. So download this one.
Insert picture description here
Address: https://www.autoitscript.com/site/autoit/downloads/

2. After decompression, send the 3 required files to the desktop

Insert picture description here
The three files needed, I thought it was separate, and there are many pits, this is unique
Insert picture description here

3. I will not write about this if I need to install Seleium, if not, I can see my other blogs

https://blog.csdn.net/qq_43107323/article/details/105423492

Four, write code with Pycharm

1. Single file upload

import os
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.layui.com/demo/upload.html')
driver.find_element_by_id("test1").click()
# 上传经过autoIT编译好的exe文件
os.system("D:\\test1.exe")

Remember: this must not be placed on the C drive, here are the 2 biggest pits:
1. Do not put on the C drive, it may not have administrator rights, it will not only not upload, but also output garbled.
2. The path must be \\ 2 lines, one will not be recognized, the \ is an escape character, so the path must always start with \\.

Fifth, use AutoIt Window Info file to capture window information

1. First open the AutoIt Windows Info tool, click the Finder Tool with the mouse, hold down the left mouse button and drag it to the control that needs to be recognized.
Insert picture description here
Insert picture description here
It can be known after identification:
the title of the window is "file upload", and the Class of the title is "# 32770".
The class of the file name input box is "Edit" and the Instance is "1", so the ClassnameNN is "Edit1".
The class of the open button is "Button" and the Instance is "1", so the ClassnameNN is "Button1".

Six, use SciTE Script Editor tool to edit

If the conditions are the same as mine, you can copy and paste, just change the path.

ControlFocus ("Window Title", "Window Text", Control ID) Set the input focus to a control in the specified window WinWait (
"Window Title" [, "Window Text" [, Timeout]]) Pause the execution of the script until ControlSetText (
"window title", "window text", control ID, "new text" ) until the specified window exists (appears) Modify the text of the specified control Sleep (delay) makes the script pause for a specified period of time
ControlClick ("window title", " Window Text ", Control ID [, Button] [, Clicks]]) Send a mouse click command to the specified control
where title is the Title field recognized by AutoIt Window Info and controlID is
the concatenation of Class and Instance recognized by AutoIt Window Info , The result after splicing as shown above should be: Button1

;ControlFocus( "窗口标题", "窗口文本", 控件ID) 设置输入焦点到指定窗口的某个控件上
ControlFocus("打开","","Edit1")
;WinWait( "窗口标题" [, "窗口文本" [, 超时时间]] )  暂停脚本的执行直至指定窗口存在(出现)为止
WinWait("打开","",10)
;ControlSetText( "窗口标题", "窗口文本", 控件ID, "新文本" )   修改指定控件的文本
ControlSetText("打开","","Edit1","C:\Users\ZF\Desktop\zf.png")
;Sleep( 延迟 )   使脚本暂停指定时间段,这里是以毫秒为单位的
Sleep(1000)
;ControlClick( "窗口标题", "窗口文本", 控件ID [, 按钮] [, 点击次数]])   向指定控件发送鼠标点击命令
ControlClick("打开","","Button1")

Insert picture description here
After writing the AutoIT script, save it. You can run the script through the menu bar "Tools"-> "Go" or F5.
Note: When running, the upload window must be open.

Seven, use Aut2exe_x64.exe for conversion

Insert picture description here
Remember: after the transfer, the .exe file must be cut to another disk.

Eight, just run the Python file directly, you can upload the file

You can use https://www.layui.com/demo/upload.html here . This URL can be tested and works very well. This is just a single file upload. In fact, the idea of ​​uploading multiple files is the same, and I will continue to improve it later.
Insert picture description here

Published 79 original articles · 321 praised · 40,000+ views

Guess you like

Origin blog.csdn.net/qq_43107323/article/details/105586203