Download and install AutoIt
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;
1. Open the official website of AutoIt to download
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools