File Upload using Selenium WebDriver and Java Robot Class

0
0

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.

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")

couldn’t be exeuted. Kindly help

  • You must to post comments
0
0

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());
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website