Your code looks correct to me. I assume that the file exists on your system. You also provide the absolute path of the file to upload which is also right. To me it looks like, the form is not getting submitted correctly. So I would recommend to use submit();
instead of click();
WebElement commitButton=driver.findElement(By.name("commit"));
commitButton.submit();
From your comment, it seems like submit();
works for FirefoxDriver()
and doesn't for HtmlUnitDriver
. I noticed that your HtmlUnitDriver
doesn't have Javascript enabled. From the documentation here, try below
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
or
HtmlUnitDriver driver = new HtmlUnitDriver(true);
Also make sure you have the latest Selenium library.