Python and Selenium and autoit how to implement the file upload function

Source: Internet
Author: User
This article mainly introduces the Python+selenium+autoit implementation of File upload function, the need for friends can refer to the following

Problem

When doing web-side UI layer Automation will encounter file upload operation, often a friend asked, here to summarize

Solution Solutions

The first type: type=file upload file, similar to the following

You can do this with code like this:


Driver.find_element (' name ', ' file '). Send_keys ('./Cockroach test brand. png ')

The second kind: is the first kind except, realizes is more troublesome, here uses the AutoIt, the approximate step is as follows:

1, download and install AutoIt, after the Start menu can see the following

AutoIt Windows info to identify Windows control information

Compile Script To.exe is used to generate EXE executable files for AutoIt

Run script for executing autoit scripts

SciTE script Editor for writing AutoIt scripts

2. Upload function as follows

3, the identification element, mainly in the file name input box and the Open button, using AutoIt Windows info to complete, recorded the results are as follows:

The file name input box is "Edit" and instance is "1"

The Open button has the class "button" and the instance is "1"

4, write scripts, using SciTE script Editor, the content is as follows:


Controlfocus ("File Upload", "" "," Edit1 ") winwait (" [CLASS: #32770] "," "," Ten) controlsettext ("File Upload", "", "Edit1", "D:python_ Workspaceqiangseautopic Qiang test brand. jpg ") Sleep (Controlclick) (" File Upload "," "," Button1 ");

In the above code, it is particularly 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 be different for each system

5. Verification Script

Ensure that 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 as 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 self-study, such as can also be used in the following ways:

Python Pywin32 Library to identify dialog handles and manipulate

SendKeys Library

keybd_event Analog keys

Summarize

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.