I work on test script to automate file uploading to a site and execute script in headless mode. It needs to upload list of files one by one, I develop it based on Selenium WebDriver
. I use AutoIT
script to handle dialog window, file chooser window. Parameter $CmdLine[1]
contains the path of actual file.
ControlFocus("Open a file","","Edit1")
ControlSetText("Open a file","","Edit1", $CmdLine[1])
ControlClick("Open a file","","Button1")
It is executed with this code:
Runtime.getRuntime().exec(autoITExecutable);
It opens dialog window, so it can't work without focus on browser window. java.awt.Robot
class works similar, it needs focus on browser window.
I tried to use sendKeys()
method too, but input field is unable to handle file in this way. Katalon Studio
is also unable to handle this field.
Example sites with similar forms:
http://ajaxuploader.com/demo/simple-upload.aspx
https://ec.europa.eu/cefdigital/DSS/webapp-demo/validation
https://tus.io/demo.html
You can try the following code:
// wait for the window to appear
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
// switch to the file upload window
Alert alert = driver.switchTo().alert();
// enter the filename
alert.sendKeys(fileName);
// hit enter
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
// switch back
driver.switchTo().activeElement();
Try this one,
webElement.sendKeys(System.getProperty("user.dir") + "file path");
Here,
Make sure, you are not clicking on the browse button, clicking on browse button will open windows dialogue box where selenium webDriver will won't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With