使用 Selenium WebDriver 和 Java Robot Class 上传文件 [英] File Upload using Selenium WebDriver and Java Robot Class

查看:36
本文介绍了使用 Selenium WebDriver 和 Java Robot Class 上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Selenium WebDriver 和 Java,我需要自动执行文件上传功能.我尝试了很多,但是当单击浏览"按钮并打开一个新窗口时,脚本停止进一步执行,而是卡住了.我在 FireFox 和 IE 驱动程序中都尝试过,但无济于事.

I am using Selenium WebDriver and Java and I need to automate the file upload feature. I tried a lot, but the moment the Browse button is clicked and a new window opens the script stops executing further and rather getting stuck. I tried in both FireFox and IE driver but to no avail.

我也尝试过调用 autoit exe 文件,但是当单击浏览按钮打开新窗口时,特定语句

I tried also by calling an autoit exe file, but as the new window opens on click of Browse button, the particular statement

Runtime.getRuntime().exec("C:\Selenium\ImageUpload_FF.exe")

无法执行.请帮忙

推荐答案

这应该适用于 Firefox、Chrome 和 IE 驱动程序.

This should work with Firefox, Chrome and IE drivers.

FirefoxDriver driver = new FirefoxDriver();

driver.get("http://localhost:8080/page");

File file = null;

try {
    file = new File(YourClass.class.getClassLoader().getResource("file.txt").toURI());
} catch (URISyntaxException e) {
    e.printStackTrace();
}

Assert.assertTrue(file.exists()); 

WebElement browseButton = driver.findElement(By.id("myfile"));
browseButton.sendKeys(file.getAbsolutePath());

这篇关于使用 Selenium WebDriver 和 Java Robot Class 上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆