Python webdriver File Upload (ii)

Source: Internet
Author: User

A way to add a file upload today

Mainly because the use of SendKeys method in the work is not stable, the concrete method see:

Python webdriver File Upload (one)

This method executes the script directly from the command line without problem and can be uploaded successfully, but if the Saltstack remote control executes, SendKeys will not be able to locate the window.

So this new way to achieve the file upload function, and the perfect solution to this problem.

The following are the steps:

1. Download tool AutoIt and use

AutoIt is currently the latest version of the V3, a freeware that uses a similar basic scripting language, designed for automated operations in the Windows GUI (graphical user interface). It uses analog keyboard keys, mouse Movement and window/control combinations to automate tasks.

Official website:https://www.autoitscript.com/site/

AutoIt v3.3.14.0 Released

  

Select Download file

      

Select the installation path, and other default installations. These apps can be found in the program after installation.

AutoIt Windows info is used to help us identify Windows control information.

Compile Script To.exe is used to generate AutoIt exe execution files.

Run script is used to execute the AutoIt script.

The SciTE script Editor is used to write AutoIt scripts.

File Upload Instance

<HTML><Head><Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8" /><title>Upload_file</title><Linkhref= "Http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"rel= "stylesheet" /></Head><Body>  <Divclass= "Row-fluid">    <Divclass= "Span6 Well">    <H3>Upload_file</H3>      <inputtype= "File"name= "File" />    </Div>  </Div></Body><Scriptsrc= "Http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></Script></HTML>

Save the above HTML code as a uplad.html file, opened in a browser, with the following effect:

Take the action upload.html upload pop-up window as an example to explain AutoIt implementation upload process.

1.1, first open the AutoIt Windows Info tool, mouse click on Finder tools, the mouse will become a small fan-shaped icon, hold down the left mouse button to drag the control to be recognized.

Get the following information by AutoIt Windows info.

The title of the window is "open" and the class of the caption is "#32770".

The file name input box has the class "Edit" and instance "1", so Classnamenn is "Edit1".

The Open button's class is "button" and instance is "1", so Classnamenn is "Button1".

1.2. Open the SciTE Script Editor and write scripts according to the control information that AutoIt Windows Info recognizes.

; The script is written in'; 'Is the comment that follows the comment, so the one-line end is not added'; 'all can; Controlfocus ("title","Window text", ControlID) sets the input focus to a control on the specified window;; Winwait ("title question","Window text", timeout) pauses execution of the script until the specified window exists (appears);; Controlsettext ("title","Window text", ControlID,"New text") To modify the text of the specified control;; Sleep (delay) causes the script to pause for a specified period of time;; Controlclick ("title","Window text", control ID, button, clicks) sends a mouse click command to the specified control, where the title is the title field identified by AutoIt window info, controlid the class that AutoIt window info recognizes and instance splicing, such as stitching after the results should be: Button1;
Title must correspond to the title of the right window,"Text"can write not write, Controlfocus ("Open","Text","Edit1") winwait ("[CLASS: #32770]","", 10); Controlsettext ("Open","","Edit1","D:\upload_file.txt"here the "text" does not have to write, write can not be executed correctly controlsettext ("Open","","Edit1","D:\upload_file.txt") Sleep (2000) Controlclick ("Open","","Button1")

The Controlfocus () method is used to identify window windows. Winwait () Sets 10 seconds to wait for the display of the window, similar in usage to the implicitly_wait () provided by Webdriver. Controlsettext () is used to enter the path to the local file in the file name input box. The sleep () method here is the same as the sleep () method provided by the time module in Python, but it is in milliseconds and Sleep (2000) represents a fixed sleep of 2000 milliseconds. Controlclick () is used to click the "Open" button in the Upload window.

Files that generate AU3 suffixes after they are saved

AutoIt's script has been written, and you can run a script from the menu bar "Tools" and "Go" (or press keyboard F5)! Note the Upload window is currently open at run time. After running, you will automatically enter the file path and automatically click "Open" to complete the file upload.

1.3. The saved script can be opened and run by the Run Script tool, but we want this script to be called by the Python program, then we need to generate the EXE program. Open the Compile Script to.exe tool and generate it as an EXE executable file.

2, Python implementation file upload

The EXE program can be invoked and executed through the system () method of the Python OS module.

The code is as follows:

Import= R"D:\Automationtest\caseData\itemRelease.exe"Os.system ( Releasefile)

Where the code in ItemRelease.exe is as follows

Controlfocus ("File Upload", "text", "Edit1"); Winwait ("[CLASS: #32770]", "", +); Controlsettext ("File Upload", "" "," Edit1 "," D:\Automationtest\caseData\itemRelease.xlsx "); Sleep (+); Controlclick ("File Upload", "", "Button1");

Code applied in the project

Python webdriver File Upload (ii)

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.