简体   繁体   English

使用Selenium和Java的文件上传功能

[英]File Upload Feature using Selenium and java

I need to automate the file upload feature in a web console and i am using selenium and java for this. 我需要在Web控制台中自动执行文件上传功能,为此我正在使用Selenium和Java。 I tried multiple approaches but when i click on upload button and windows explorer gets opened, it stops there. 我尝试了多种方法,但是当我单击上载按钮并且打开Windows资源管理器时,它就在那里停止。 doesn't select any file...and gives error that there is no file.. I tried in firefox and chrome both but i am not able to solve this problem. 没有选择任何文件...并给出了没有文件的错误。.我在Firefox和chrome中都尝试过,但是我无法解决此问题。

Then i also tried AutoIt tool. 然后我也尝试了AutoIt工具。 I downloaded it and made a script. 我下载并制作了脚本。 trying to compile my script i am getting this error: 尝试编译我的脚本时出现此错误:

Code I'm using: 我正在使用的代码:

WebDriver driver = new FirefoxDriver(); 
driver.get("localhost:8080/page"); 
WebElement selectUploadApk = driver.findElement(By.id("id of upload button"));
selectUploadApk.click();
WebElement file = driver.findElement(By.xpath("//input[@type='file']")); 
file .sendKeys("path of the file");

Error: Unable to execute upx.exe to compress stub file File not found Exception 错误:无法执行upx.exe压缩存根文件找不到文件异常

Please help 请帮忙

Thanx in advance 提前感谢

Megha 梅加

You don't need to click on the field to open the dialogue box. 您无需单击该字段即可打开对话框。

Opening the dialogue box is what is 'breaking' your test. 打开对话框将“破坏”您的测试。

Just send the keys directly to the input element, as you are, and then click on which ever button is the 'upload' button. 只需按原样将键直接发送到输入元素,然后单击哪个按钮是“上载”按钮即可。

  driver.findElement(By.xpath("//input[@type='file']")).sendKeys("/path/to/file");
  driver.findElement(By.id("id of upload button")).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM