英文:
Selenium Driver / Java - Upload File Without <input type="file">
问题
我知道使用Selenium上传文件的方法如下:
WebElement UploadImg = driver.findElement(By.xpath("//input[@id='file_upload_button']"));
UploadImg.sendKeys("d:\.jpg");
然而,这需要文件上传按钮位于 <input type="file" id="file_upload_button">
表单中。
我可以点击一个上传按钮,然后看到Windows文件提示窗口。在选择文件并点击“打开”之后,它可以工作。
似乎只有这种方式起作用。我需要能够在这里选择文件。我已经尝试了所有的按键组合。但是,似乎没有任何方法可以让我编辑Windows文件提示窗口顶部的文件名?
有哪些解决方法?如果能够附带一些使用Java的示例,将不胜感激。
英文:
I am aware of this way to upload a file using Selenium
WebElement UploadImg = driver.findElement(By.xpath("//*[@id='file_upload_button']"));
UploadImg.sendKeys("d:.jpg");
However, it requires the file upload button to be a in the <input type="file" id="file_upload_button">
form.
I can click an upload button and see the windows file prompt window. After selecting file and clicking "Open", it works.
It only seems to work this way. I need to be able to select the file here. I have tried all the keystroke combinations. But, nothing seems to let me edit the file name at the top of the windows file prompt window?
What are the solutions to this? Some great examples with Java would be greatly appreciated.
答案1
得分: 0
我已经使用 AutoIt Windows Automation 解决了这个问题。
我们可以使用 Java 和 Selenium Web Driver 来点击网站上的上传按钮。
然后,我们可以调用在 AutoIt 中编写的 .exe 文件。在我们的 Java 应用程序内部。来执行在出现的窗口中进行必要操作的操作。这些操作超出了 Selenium Web Driver 的范围。
我通过这个 YouTube 视频 学习了 AutoIt 的基础知识。
我对所学内容进行了一些编辑以适应我的需求。在我们的 Java 应用程序中,我们可以将要上传的文件路径写入一个 .txt 文件中。然后 AutoIt 的 .exe 文件在运行时引用此文件。这样,我们就不必将文件路径硬编码到 .exe 文件中。这意味着我们可以在运行时进行更改。
AutoIt 代码
此代码从 .txt 文件中读取我们要上传的文件的路径。
然后它聚焦于窗口弹出窗口中的文件路径文本框。并将文件路径字符串粘贴到其中。
然后点击“打开”按钮。
#include <File.au3>
# .txt 文件的文件路径和文件名
Global $sFileName = @ScriptDir & "\info.txt"
# 从 .txt 文件中获取文件名
Global $sFileRead = FileRead($sFileName)
# 聚焦于弹出窗口中的文件路径文本框
ControlFocus("Open", "", "Edit1")
# 短暂延迟
Sleep(1000)
# 将文件名输入到弹出窗口的文本框中
ControlSetText("Open", "", "Edit1", String($sFileRead))
# 短暂延迟
Sleep(1000)
# 点击上传按钮
ControlClick("Open", "", "Button1")
Java 代码
此代码将要上传的文件写入 .txt 文件中。
然后在网页浏览器内点击上传按钮。
然后调用我们的 AutoIt .exe 文件来运行我们的脚本。执行 Selenium Web Driver 无法完成的操作。
从而成功选择和上传了正确的文件。
// 使用 PrintWriter 获取文件
PrintWriter writer = new PrintWriter("E:/Google Drive/Data - Cloud/Programming/AutoIt/info.txt", "UTF-8");
// 将文件路径添加到文本文件中
writer.print("C:\\Users\\jacktrow\\Pictures\2.jpg");
// 关闭 PrintWriter
writer.close();
// 在目标站点上点击上传按钮
driver.findElement(By.xpath("//a[@data-trigger='anywhere-upload-input']")).click();
// 运行使用我们在 .txt 文件中保存的文件路径的 AutoIt .exe 脚本
Runtime.getRuntime().exec("E:/Google Drive/Data - Cloud/Programming/AutoIt/test.exe");
英文:
I have managed to solve this using AutoIt Windows Automation.
We can use Java and Selenium Web Driver to click an upload button on a website.
Then we can call a .exe file written in AutoIt. Inside our Java app. To do the necessary operations in the window that appears. Operations outside the scope of Selenium Web Driver.
I learned the fundamentals of AutoIt with this YouTube video
I made some edits to what I was taught. To fit my needs. In our Java app, we can write our file path we want to upload into a .txt file. This file is then referenced by the AutoIt .exe file in runtime. This way we're not having to hard code our file path into the .exe file. It means we can change it in runtime.
AutoIt Code
This code reads the path of the file we want to upload from a .txt file.
Then it focuses on the file path text box in the windows popup window. It pastes this file path string inside it.
Then it clicks the "Open" button.
#include <File.au3>
# File path of .txt with file name inside
Global $sFileName = @ScriptDir & "\info.txt"
# Get file name from .txt file
Global $sFileRead = FileRead($sFileName)
# Focus in on file path text box in pop up window
ControlFocus("Open","","Edit1")
# Small delay
Sleep(1000)
# Enter file name into text box in popup window
ControlSetText("Open","", "Edit1", string($sFileRead))
# Small delay
Sleep(1000)
# Click the upload button
ControlClick("Open", "", "Button1")
Java Code
This code writes the file we want to upload to a .txt file.
Then it clicks the upload button inside the web browser.
Then it calls our AutoIt .exe file to run our script. To do the operations Selenium Web Driver can't do.
This results in the right file being successfully chosen and uploaded.
// Get file with PrintWriter
PrintWriter writer = new PrintWriter("E:/Google Drive/Data - Cloud/Programming/AutoIt/info.txt", "UTF-8");
// Add our file path to the text file
writer.print("C:\\Users\\jacktrow\\Pictures\2.jpg");
// Close PrintWriter
writer.close();
// Click upload button on target site
driver.findElement(By.xpath("//a[@data-trigger='anywhere-upload-input']")).click();
// Run AutoIt .exe script that uses file path we saved in .txt file
Runtime.getRuntime().exec("E:/Google Drive/Data - Cloud/Programming/AutoIt/test.exe");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论