Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Python+selenium+autoit implements file upload

Shulou Source: shulou.com Published: 2022-06-03 05:22:38 09月24日 Update

problem

When you automate the ui layer on the web side, you will encounter the operation of uploading files. Some friends often ask, "here's a summary."

Solution

First: upload files of type=file, similar to the following

You can do this with code like this:

Driver.find_element ('name','file'). Send_keys ('. / Xiaoqiang Test Brand. PNG')

The second is the one except the first, which is difficult to implement. Autoit is used here, and the general steps are as follows:

1. Download and install autoit, and then you can see the following on the start menu

AutoIt Windows Info is used to identify Windows control information

Compile Script to.exe is used to generate exe execution files from AutoIt

Run Script is used to execute AutoIt scripts

SciTE Script Editor is used to write AutoIt scripts

2. Upload functions are as follows

3. Identify the elements, mainly the file name input box and the open button in the image above, which are completed with AutoIt Windows Info. The recording result is as follows:

The class of the file name input box is "Edit" and the Instance is "1"

The class of the open button is "Button" and the Instance is "1"

4. Write a script using SciTE Script Editor, as follows:

ControlFocus ("File upload", "", "Edit1")

WinWait ("[CLASS:#32770]", "", 10)

ControlSetText ("File upload", "Edit1", "D:python_workspaceQiangSEAutopic Xiaoqiang Test Brand .jpg")

Sleep (2000)

ControlClick ("File upload", "", "Button1")

In the above code, it is important to note that the word "file upload" is the title of the dialog box that pops up after you click the upload button, which may vary from system to system.

5. Verify the script

Make sure the upload dialog box for the page opens, and then run the script tools > go

6. Open the Compile Script to.exe tool and generate it into an exe executable file

7. Call in python script

Up=self.driver.find_element ('class name','avatar-uploader-trigger')

Up.find_element ('class name','ant-btn') .click ()

Os.system ('D:\ python_workspace\ QiangSEAuto\ upload.exe')

Time.sleep (20)

Other

In fact, there are other solutions, interested in their own research, for example, you can also use the following ways:

Python pywin32 library, identify dialog box handles, and then manipulate

SendKeys library

Keybd_event Analog Button

Tags: Files scripts dialogs buttons dialogs codes brands Xiaoqiang file names tests inputs pictures information elements interests content functions handles words that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Information Shulou Technology Microsoft MariaDB