简体   繁体   English

如何使用Selenium WebDriver处理Firefox上载文件窗口-Java

[英]How to handle Firefox Upload File window with Selenium WebDriver - Java

I know this topic is a duplicate but , I have applied other solutions too , and as an outcome I have a problem about sending the correct key to correct place for File Upload window. 我知道这个主题是重复的,但是,我也应用了其他解决方案,结果我在发送正确的密钥到“文件上传”窗口的正确位置时遇到了问题。

I have used this piece of code to Open and send keys to Upload File: 我已使用这段代码来打开并将密钥发送到上载文件:

WebElement fileInput = driver.findElement(By.id("upload-resume-button"));
fileInput.sendKeys("C:/Users/EvrenosCareer/Desktop/filename.pdf");

By sending keys correct button gets activated and File Upload window opens as expected , but keys was send as something else , and it appears down left corner of the browser; 通过发送密钥,正确的按钮将被激活,“文件上传”窗口将按预期方式打开,但是密钥却以其他方式发送,并且出现在浏览器的左下角; not File upload window. 不是文件上传窗口。 Please check this link , 5 sec video to see exactly what I am talking about: https://evrenos-hotmail.tinytake.com/sf/MTcxNDY0N181Njg2OTY1 请查看此链接(5秒的视频)以准确了解我在说什么: https : //evrenos-hotmail.tinytake.com/sf/MTcxNDY0N181Njg2OTY1

You can directly do fileInput.sendKeys("C:/Users/EvrenosCareer/Desktop/filename.pdf") if the type of the element is "file" else you will have to upload file using Robot class. 如果元素的类型为“文件”,则可以直接执行fileInput.sendKeys(“ C:/Users/EvrenosCareer/Desktop/filename.pdf”) ,否则您将不得不使用Robot类上载文件。 You don't have to click on fileInput to send keys on that element. 您不必单击fileInput即可在该元素上发送键。

Upload file through Robot class: 通过Robot类上传文件:

 WebElement element = driver.findElement(By.id("upload-resume-button"));
 element.click();
StringSelection stringSelection = new StringSelection("path to File");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
 Robot robot = new Robot();
 robot.keyPress(KeyEvent.VK_CONTROL);
 robot.keyPress(KeyEvent.VK_V);
 robot.keyRelease(KeyEvent.VK_V);
 robot.keyRelease(KeyEvent.VK_CONTROL);
 robot.keyPress(KeyEvent.VK_ENTER);
 robot.keyRelease(KeyEvent.VK_ENTER);

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

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