How should We use Selenium to upload files when we encounter the implementation of file uploading in Web automation?

The label type is INPUT and directly uploaded to Selenium

When we do Selenium automation, we must face the location of page elements, if the upload file function tag type is INPUT, for example:

We can directly use selenium elements to locate the +send_keys() method, passing the file path in parentheses, for example:

driver.find_element_by_xpath(“”).send_keys(r”c:/test.png”)

The above is uploaded directly using Selenium for elements of type INPUT.

Second, the label type is not INPUT, use the tool AutoIT upload

AutoIT is a similar scripting language software, you can locate and operate the mouse, keyboard, window, etc., write it into a file, and then convert it into a computer executable EXE file.

Download address: https://www.autoitscript.com/site/autoit/

Autoit can be found in all computer programs after installation:

**AutoIt Window Info: ** Then open the page element upload control and use AutoIt Window Info to locate

Title= “open”, Class = Button, Instance=1, Class = #32770 We use this information of controls to locate controls and write scripts.

SciTE Script Editor

Search for SciTE Script Editor in all programs, open the software, and edit the related control contents in it:

**ControlFocus: ** Sets the focus to a control in a window

**WinWait: ** Sets the timeout until an element coordinate appears

**ControlSetText: ** Modifies the text information of the specified control and adds the path to the uploaded file

**ControlClick: ** Initiate instructions to the mouse to operate the control

**sleep: ** Sets the wait time

ControlFocus("Open".""."Edit1"); Identify the Windows window WinWait("[CLASS:#32770]"."", 10); Window wait ten seconds ControlSetText("Open".""."Edit1"."C:\User\sergou\Desktop\picture\1.png"); Sleep(2000) ControlClick("Open".""."Button1"); ; Click on the [open] button to 123456789101112131415161718Copy the code

1079636098 Python Software Test Automation Communication Group: 1079636098 Software Test, with you!

After the script is written, you can run the script on the menu bar “Tools” > “Go” to debug the script and view the effect. Note that the upload window should be open when running.

Exe in all programs of the computer to find the Complie Script to. Exe software, to edit the above saved text to be translated into computer executable exe file

Click Browse to select the saved text and click Convert to generate an exe executable file. The above is the process of uploading files using Autoit.

python+selenium+autoit

Next we need to use Python + Selenium + Autoit to upload files in conjunction with Web automation.

We need to locate the upload space element and click on it, then use python’s os.system() method to execute the exe file we prepared in advance.

Third, through the interface request, easily realize the automatic file upload function

In the process of automatic testing, if UI automation is difficult to implement certain service scenarios, you can use interfaces and UI automation to meet the requirements of automatic testing scenarios

For example, uploading files can be done via Selenium or Autoit, but it is recommended that the automation of file upload types be easily implemented by invoking the file upload interface

As shown: Postman invokes the file upload interface

It is also easy to automate the file upload interface with Python + Request

Finally: the more information you can join us author: test Wang Daniu reference: https://www.bilibili.com/read/cv6955031 bilibili