Python+selenium+autoit implementing file Uploads

Source: Internet
Author: User
Tags python script

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

650) this.width=650; "src=" Http://p3.pstatp.com/large/322c0000ec9ccd6d16e9 "alt=" python+selenium+autoit implement File Upload " style= "border-style:none;margin:10px auto;"/>

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

650) this.width=650; "src=" http://p9.pstatp.com/large/32120003bd8797e6513c "alt=" python+selenium+autoit implement File Upload " style= "border-style:none;margin:10px auto;"/>

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

650) this.width=650; "src=" http://p1.pstatp.com/large/322d00038f0be9414e2c "alt=" python+selenium+autoit implement File Upload " style= "border-style:none;margin:10px auto;"/>

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]", "", 10)

Controlsettext ("File Upload", "", "Edit1", "d:python_workspaceqiangseautopic cockroach test brand. jpg")

Sleep (2000)

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

650) this.width=650; "src=" http://p1.pstatp.com/large/321000043d11d1f8d04b "alt=" python+selenium+autoit implement File Upload " style= "border-style:none;margin:10px auto;"/>

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


This article is from the "Cockroach Performance automation test brand" blog, please be sure to keep this source http://xqtesting.blog.51cto.com/4626073/1958454

Python+selenium+autoit implementing file uploads

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.