使用Selenium WebDriver和Java Robot Class进行文件上载 [英] File Upload using Selenium WebDriver and Java Robot Class
本文介绍了使用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文件,但是当点击Browse按钮打开新窗口时,特定声明
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屋!
查看全文