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]", "", 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
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
Python+selenium+autoit implementing file Uploads