Go to Blog: Shanghai-leisurely
Objective
AutoIt implementation of the file upload package as an. exe executable file, each time can only pass the fixed image, we actually test when we want to pass a different picture.
In this way, each time you call, you can add a file path parameter to the command line.
One, command-line parameters
1. Parameterize the passed-in parameters by AutoIt the command-line arguments:
```
MyProg.exe param1 "This is a string parameter" 99
```
In the script, command-line arguments can be obtained using the following variables:
```
$CmdLine [0]; = 3
$CmdLine [1]; = param1
$CmdLine [2]; = "This is a string parameter"
$CmdLine [3]; = 99
$CmdLineRaw; = ' param1 ' This is a string parameter ' 99 '
```
-$CmdLine [0] Gets the total number of command-line arguments, in the example above $cmdline[0]=3
-$CmdLine [1]~ $CmdLine [63] gets command-line arguments 1th through 63rd, which can fetch up to 63 parameters, but is normally sufficient
-$CmdLineRaw gets all the parameters that are not split, is a long string, in which case it is not limited to 63 parameters
2. After saving the following code as an. exe file (file name randomly: Cmdjpg.exe), execute once in CMD to see if it succeeds
```
Winactivate ("File Upload");
Controlsettext ("File Upload", "", "Edit1", $CmdLine [1]);
Sleep (2000);
Controlclick ("File Upload", "", "Button1");
```
3. Execute with Python code
"' Python
# The path of the image to be uploaded
File_path = "D:\\1.png"
# Execute AutoIt Upload file
Os.system ("C:\Users\Gloria\Desktop\cmdjpg.exe%s"% file_path) # your own local
```
Second, batch upload pictures
1. Method One: First put the image to be uploaded under a list, and then for loop
"' Python
# Put the images you need to upload into a list
All_png = ["D:\\1.png", "D:\\2.png", "D:\\3.png", "D:\\4.png"]
# cycle Click to upload images
For I in All_png:
# 1 O'Clock Open editor picture
# 2 O'Clock Open File Upload button
# Execute AutoIt Upload file
Os.system ("C:\Users\Gloria\Desktop\cmdjpg.exe%s"% i) # your own local. exe path
Time.sleep (3)
```
2. Method Two: To upload the image number, such as: 0.png, 1.png, 2.png this (starting from 0 numbering), put in the same directory, and then for loop
"' Python
# cycle Click to upload images
For I in range (4):
# 1 O'Clock Open editor picture
# 2 O'Clock Open File Upload button
# file name
file_name = "D:\\%s.png"% i # parameterized path name
# Execute AutoIt Upload file
Os.system ("C:\Users\Gloria\Desktop\cmdjpg.exe%s"% file_name) # your own local. exe path
Time.sleep (3)
```
Third, reference code
"' Python
# Coding:utf-8
From selenium import Webdriver
Import time
Import OS
# load Profile Implementation Free login
Profiledir = R ' C:\Users\Gloria\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default ' # The profile path of your own computer, do not copy my
Profile = Webdriver. Firefoxprofile (Profiledir)
Driver = Webdriver. Firefox (Profile)
Driver.implicitly_wait (30)
Driver.get ("http://www.cnblogs.com/yoyoketang/")
Driver.find_element_by_link_text ("new essay"). Click ()
Time.sleep (3)
# method One: Put the images that need to be uploaded to a list
All_png = ["D:\\1.png", "D:\\2.png", "D:\\3.png", "D:\\4.png"]
For I in All_png:
# Click to open Editor picture
Driver.find_elements_by_css_selector ("Img.mceicon") [0].click ()
Time.sleep (3)
# Locate all the IFRAME and take a second
iframe = Driver.find_elements_by_tag_name (' iframe ') [1]
# Switch to the IFRAME
Driver.switch_to_frame (IFRAME)
# Click to open the File upload button
Driver.find_element_by_name (' file '). Click ()
Time.sleep (3)
# Execute AutoIt Upload file
Print I
Os.system ("C:\Users\Gloria\Desktop\cmdjpg.exe%s"% i) # your own local. exe path
Time.sleep (3)
Driver.switch_to_default_content () # cut back to the main page
# # Method Two: Cycle Click Upload Image
# for I in range (4):
# # 1 O'Clock Open editor picture
#
# # 2 O'Clock Open the File upload button
#
# # File name
# file_name = "D:\\%s.png"% i # parameterized path name
#
# # Execute AutoIt Upload file
# Os.system ("C:\Users\Gloria\Desktop\cmdjpg.exe%s"% file_name) # your own local. exe path
# Time.sleep (3)
```
Selenium+python Automation 78-autoit parameterization and batch upload "reprint"