File uploading is a tricky part of automation. Currently selenium does not provide an upload implementation API, so you know to use external forces to complete it, such as AutoIt and sikuli.
AutoIt, this is a free software that uses a BASIC-like scripting language. It is designed for automating Windows GUI (graphical user interface) operations by simulating keyboard keys, mouse movements, and window/control combinations. Automation tasks;
Both download methods are available. The one I want to download here is zip. Unzip it as shown below Shown:
Click on the SciTe folder and we open the script editor.
Open the Baidu image upload window, open the AutoIt Windows Info tool, move the mouse to the Finder Tool, hold down the left mouse button and drag it to the windows control that needs to be identified. Drag the target shape button on the element locator to the file upload pop-up window to capture some element information. Use the mouse to drag the Finder Tool icon on the tool (that is, the blue circled part in the picture) to the control to be identified. The unique identification information of the control will be displayed on the left part of the tool (the part marked by the red box in the picture) ). From the displayed results, we know that the Title of this control is "Open", the Class is Edit, and the Instance=1. We use this information of the control to locate the control and write scripts.
Open the editor and call the function to write the script based on the information recognized by the control Finder Tool; enter the following script in the AutoIt script editor without the remarks I wrote below. .
We need to know the following information here:
1. The title of the operation page, which is used to fix the operation page.
2. For the information that needs to be filled in, fill in the "path and file name of the uploaded file" in the input box (windows operation)
3. Click the "Open" button to upload the file.
Based on the control information identified above, use the editor SciTE Script Editor to write scripts according to the syntax of AutoIT.
Several methods are needed to implement file upload:
ControlFocus ( "窗口标题", "窗口文本", 控件ID) ---->设置输入焦点到指定窗口的某个控件上(即:控件ID“文件名”输入框的id) WinWait ( "窗口标题" [, "窗口文本" [, 超时时间]] ) ---->暂停脚本的执行直至指定窗口存在(出现)为止 ControlSetText ( "窗口标题", "窗口文本", 控件ID, "新文本" ) ---->修改指定控件的文本(即:控件ID“文件名”输入框的id) Sleep ( 延迟 ) ---->使脚本暂停指定时间段 ControlClick ( "窗口标题", "窗口文本", 控件ID [, 按钮] [, 点击次数]] ) ---->向指定控件发送鼠标点击命令(即:控件ID“打开”按钮的id)
Among them, title is the Title field recognized by AutoIt Window Info, and controlID is the splicing of Class and Instance recognized by AutoIt Window Info, as above The result after image splicing should be: Button1 (i.e. classnameNN)
ControlFocus(("title","text",controllD)用于识别windows文件上传窗口 ControlFocus("打开","","")向文件名输入框输入本地要上传文件的路径 ControlSetText("打开","","Edit1","C:\Users\DELL\Desktop\test\upload\北京宏哥.jpeg") Sleep(2000)点击上传窗口中的“打开“按钮 ControlClick("打开","","Button1")
Save the script file in ChromFileUpload.au3 format, and then click the Tools menu in the AutoIt script editor, tools =>go, perform script verification (provided that the windows window must be open), the verification is successful, as shown in the following figure:
In order for this script to be called by the java program, it needs to use the Compile Script to .exe (x64) tool Generate an exe file (this is AutoIt installed through the .exe installation package)
Click the Tools menu in the AutoIt script editor and select compile, and an .exe file will be generated in the same path (this is installed through the unzipped package) Installed AutoIt)
# Prompt Conversion complete: Copy ChromeFileUpload.exe to the project and use it in the Selenium script later.
javacode
//实现文件上传。通过Runtime的静态方法获取Runtime对象 Runtime runtime = Runtime.getRuntime(); //通过Runtime对象调用exe方法 runtime.exec("C:\Users\DELL\Desktop\test\upload\ChromeFileUpload.exe");
The above is the detailed content of How Java Selenium uses sendkeys to upload files. For more information, please follow other related articles on the PHP Chinese website!