Table of Contents
Download and install AutoIt
1. Open the official website of AutoIt to download
Home Java javaTutorial How Java Selenium uses sendkeys to upload files

How Java Selenium uses sendkeys to upload files

Apr 20, 2023 pm 10:46 PM
java selenium

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

How Java Selenium uses sendkeys to upload files

Both download methods are available. The one I want to download here is zip. Unzip it as shown below Shown:

How Java Selenium uses sendkeys to upload files

Click on the SciTe folder and we open the script editor.

How Java Selenium uses sendkeys to upload files

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.

How Java Selenium uses sendkeys to upload files

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)
Copy after login

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)

How Java Selenium uses sendkeys to upload files

ControlFocus(("title","text",controllD)用于识别windows文件上传窗口
ControlFocus("打开","","")向文件名输入框输入本地要上传文件的路径
ControlSetText("打开","","Edit1","C:\Users\DELL\Desktop\test\upload\北京宏哥.jpeg")
Sleep(2000)点击上传窗口中的“打开“按钮
ControlClick("打开","","Button1")
Copy after login

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)

How Java Selenium uses sendkeys to upload files

# 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");
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles