Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python+selenium+autoit implements file upload

2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report