Python + selenium + autoit implements the file upload function,

Source: Internet
Author: User

Python + selenium + autoit implements the file upload function,

Problem

When automating the web ui Layer, you may encounter file upload operations. Some friends often ask, so here is a summary.

Solution

First: type = file Upload file, similar to the following

The following code can be used:

Driver. find_element ('name', 'file'). send_keys ('./Xiaoqiang test brand .png ')

The second type is exclusive to the first type, which is difficult to implement. autoit is used here, and the general steps are as follows:

1. Download and install autoit. Then, you can see the following in the Start Menu:

AutoIt Windows Info is used to identify Windows Control Information

Compile Script to.exe is used to generate an exe execution file for AutoIt.

Run Script is used to execute the AutoIt Script

SciTE Script Editor is used to compile AutoIt scripts

2. the upload function is as follows:

3. The identification element is mainly the file name input box and the Open button, which are completed using AutoIt Windows Info. The record results are as follows:

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

The class that opens the Button is "Button", and the Instance is "1"

4. Compile the Script and use SciTE Script Editor. The content is as follows:

ControlFocus ("File Upload", "", "Edit1") WinWait ("[CLASS: #32770]", "", 10) ControlSetText ("File Upload ","", "Edit1", "D: python_workspaceqiangseautopicxiaoqiang test brand .jpg") Sleep (2000) ControlClick ("File Upload", "", "Button1 ");

In the above Code, note that the "File Upload" text is the title of the dialog box that pops up after you click the upload button. Each system may be different.

5. Verify the script

Make sure that the upload dialog box is displayed, 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)

Others

In fact, there are other solutions. If you are interested, study them on your own. For example, you can use the following methods:

Python pywin32 library, identify the dialog box handle, and then operate

SendKeys Library

Keybd_event analog button

Summary

The above section describes how to upload files using python + selenium + autoit. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.