Selenium+python Automation 77-autoit File Upload "reprint"

Source: Internet
Author: User

Objective

About non-input file upload, click the upload button, this pop-up windows control, has jumped out of the three boundaries, not part of the jurisdiction of selenium (selenium is not omnipotent, can only manipulate elements on the web). The AutoIt tool handles Windows with a control window that is professional, so this needs to be solved with the help of AutoIt.

First, the Environment preparation

1. Can download AutoIt official website, install http://www.autoitscript.com/site/

2. Download to local after the installation of the Fool, after installation in the application to find this AutoIt v3

3.AutoIt inside several menu function introduction:

    • SciTE Script Editor, where you write AutoIt scripts
    • AutoIt Windows info Element Locator for knowledge of Windows control information
    • Run script executes AutoIt scripts
    • Compile Script To.exe will autoit build the. exe executable file

4.autoit online documentation, Chinese version http://www.autoitx.com/Doc/

Second, the implementation of the script

1. First prepare the environment of the Web page, take the blog Park upload image as an example: New blog > Point image upload > Upload local image > Pop up select Image interface, don't move here.

2. Open the SciTE Script Editor and start writing scripts, the code is very simple only four lines

WinActivate("文件上传");ControlSetText("文件上传", "", "Edit1", "D:\1.png" );Sleep(2000);ControlClick("文件上传", "", "Button1");

3. After the completion of the editing, Tools>go, or press F5 execution, after the execution, you can see the picture uploaded successfully.

4.autoit a few common syntax

    • Winactivate ("title") focuses on the specified active window
    • Controlfocus ("title", "Window Text", ControlID) sets the input focus to a control on the specified window;
    • Winwait ("title", "Window text", time-out) pauses execution of the script until the specified window exists (appears);
    • Controlsettext ("title", "Window text", ControlID, "new text") modifies the text of the specified control;
    • Sleep (delay) causes the script to pause for a specified time, in milliseconds;
    • Controlclick ("title", "Window text", Control ID, button, click Count) sends a mouse click command to the specified control;
Iv. positioning of elements

1.Find Tool View element properties, hold the icon under the Find tool with your mouse and drag it to the element you want to position

2. View the title field identified by Title,title, AutoIt Window Info

3. View Controlid,controlid-AutoIt Window info Identifies the concatenation of class and instance

If the class property of the "open" button here is the Button,instance property is 1, then the ControlID property is Button1

V. Export as EXE file

1. After successful execution of the third step, save the script to the local

2. In the application, find the Open compile Script to.exe tool to convert the. au3 file you just exported to an. exe file

3. In order to verify that the generated. exe file is valid, first execute it once in cmd

Simply drag the generated. exe file into cmd and the carriage return will execute

Vi. execution of Python

1. The action of uploading the file has been made into an. exe file, the next step is to execute the. exe file with Python to upload the file.

Python calls DOS, using this method Os.system ("commands to execute")

# Coding:utf-8From seleniumImport WebdriverImport timeImport OS# load config file for free login Profiledir=R ' C:\Users\Gloria\AppData\Roaming\Mozilla\Firefox\Profiles\1x41j9of.default '# Your own Computer configuration file path, do not copy my profile= Webdriver. Firefoxprofile (Profiledir) driver= Webdriver. Firefox (Profile) driver.implicitly_wait () Driver.get ("http://www.cnblogs.com/yoyoketang/") Driver.find_element_by_link_text ("New essay"). Click () Time.sleep (3)# point open editor Picture driver.find_element_by_css_selector ( "Img.mceicon"). Click () time.sleep (3)  # locate all IFRAME, take a second iframe = driver.find_elements_by_tag_name (  "iframe") [1] # switch to iframe driver.switch_to_frame (iframe)  # point to open the File Upload button driver.find_element_by_name ( File '). Click () # execute autoit Upload file Os.system (" C:\Users\Gloria\Desktop\sendjpg.exe ") # Your own local this. exe file absolute path               
Vii. autoit Command Line arguments

1. The above-packaged EXE file to upload the path of the file to write dead, each time can only pass the fixed image, we actually test when we want to pass a different picture, so you need to parameterize the file path.
To parameterize the passed-in parameters, you can pass the AutoIt command-line arguments:

    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 the command line parameter 1th to 63rd bit, this way can only get 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

WinActivate("文件上传");ControlSetText("文件上传", "", "Edit1", $CmdLine[1] );Sleep(2000);ControlClick("文件上传", "", "Button1");

2. The problem comes, if you want to upload pictures in bulk, how to achieve it?? It is not easy after parameterization.

Selenium+python Automation 77-autoit File Upload "reprint"

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.