(Twenty-five) WebDriver API to upload the file

Essays and records to facilitate their access to fellow travelers.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install their own search for other information,

Here do not introduced, all examples use python3.6 + selenium execution.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

 

upload files

  Upload files is more common Web one of the functions, but WebDriver does not provide a method specifically for uploading, upload the key is how the idea to upload files.

  General Web after the upload page of the operation need to click the "Upload" button to open the local Windows window. Select from a local file upload window. The WebDriver is inoperable Windows controls, so for beginners, general ideas get stuck in how to recognize Windows on the control issue.

  For the Web upload feature pages generally have achieved it in two ways.

  1, ordinary Uploads: Upload a common accessory is the path to a local file as a value on the input tab through from the server to the value of submitting this form.

  2, plug-in to upload: generally refers to based on Flash , JavaScript or Ajax upload technology implemented.

  A, send_keys to upload

  For through input upload tag implementation, it can be seen as an input box, i.e. through send_keys () targeting the local file path file uploads.

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form>
        <input type="file" name="file" value="上传文件">
    </form>
</body>
</html>

 

  Effects browser opens below:

 

# Normal upload files 
from Selenium Import the webdriver 

Driver = webdriver.Chrome () 
driver.implicitly_wait ( 10 ) 

driver.get ( ' C: \\ \\ 86136 PycharmProjects the Users \\ \\ \\ updata_file.html Spider ' ) 

# positioned upload button to upload local files 
driver.find_element_by_name ( ' file ' ) .send_keys ( " d: \\ App_name.txt " )

  Show results:

 

 

  In this way uploading, avoiding operating Windows step control. If the uploaded find input tag, so basically it can send_keys () method of a file to which the input address upload embodiment.

 

  Two, AutoIt to upload

 

  AutoIt latest version is v3 , it is similar to using a BASIC freeware scripting language, which is designed to be Windows the GUI (Graphical User Interface) automation testing. It uses simulating keystrokes, mouse movements and window / to automate the control task combination.

 

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

 

  Downloaded from the official website AutoIt and installation, the installation is complete, you will be seen in the menu shown below AutoIt menu:

 

  Windows info AutoIt : used to identify the Windows control information.

 

  Script to.exe the Compile : for AutoIt generate exe to execute the script.

 

  Script RUN : used to perform AutoIt script.

 

  Script Editor SciTE : for writing AutoIt script.

 

  The following is to operate above HTML code as an example to explain the AutoIt upload process.

  1. First, open AutoIt Windows info tool, use the mouse to click the Finder Tool , a small mouse icon will turn into a fan shape, as shown below:

  

 

 

 After opening the way:

  

 

  Press and hold the left mouse button, drag to the control to be identified (such as: "Open" button control) as shown below:

  

 

 

 

  Enter the file name box class for the " Edit " Instance is " 1 ", so ClassnameNN as " Edit1 " button to open the class as " the Button " Instance is " 1 ", so ClassnameNN as " Button1 "

  

 

 

  The AutoIt Windows info identified to control information, open SciTE Script Editor editor, write AutoIt script. Enter the following script:

 

;ControlFocus("title","text",controlID) Editel=Edit instance 1
ControlFocus("打开","","Edit1")

;wait 10 seconds for the Upload window to appear
WinWait("[CLASS:#32770]","",10)

;Set the File name text on the Edit field
ControlSetText("打开","","Edit1","d:\App_name.txt")
Sleep(2000)

;click on the open button
ControlClick("打开","","Button1");

  ControlFocus () method is used to identify Windows window. WinWatit () method sets 10 seconds, waiting for the display window. ControlSetText () method used to enter the local path to upload files to the "File name" input box. Here Sleep () method with the python in the time module provides Sleep () as usage method, but it is in milliseconds, Sleep (2000) represents a fixed Sleep 2000 milliseconds. ControlClick () is used to upload window, click the "Open" button.

  AutoIt script already written, by the menu bar " Tools " > " Go " (or F5 to run the script).  

  

 

 

 

  Note: In the long run should transfer window is open. 

  3. Run the script to work, save it as file.au3 file. The script can be saved here by Run Script tool to open it run, but our aim is to hope that the script is python caller, name needs to be generated as exe program. Open Compile Script to.exe tool, which is generated as exe executable file, as shown below:

  

 

 

 

  Click " the Browse " button, select file.au3 file, click " Convert For " button to become raw file.exe program.

  4. Next call by automated test scripts file.exe program to upload.

 

 

 

Selenium ROM Import the webdriver
 Import OS 
Driver = webdriver.Chrome () 
driver.implicitly_wait ( 10 ) 

# open upload page 
driver.get ( ' C: \\ \\ PycharmProjects the Users 86 136 \\ \\ \\ updata_file.html Spider ' ) 

# click open to upload window 
driver.find_element_by_name ( ' File ' ) .click () 

# call file.exe Uploader 
os.system ( " d: \\ file.exe " )

  Through the system () can be called and executed method file.exe program.

  Although this approach can solve the operational problems file upload (or download files), but I do not recommend this solution because by python called exe program is not in python controllable range. In other words , exe execution for how long, whether an error occurred, Python programs are not known.

 

Guess you like

Origin www.cnblogs.com/lirongyang/p/11459684.html