8

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

plaidshirt
  • 5,189
  • 19
  • 91
  • 181

3 Answers3

1

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();
  • I see, but if file chooser window is not opened, there is no place to paste file path. – plaidshirt Nov 12 '18 at 06:32
  • When you click on ‘Choose File’ button, OS native file select dialog will be opened. This dialog will not be recognized by your driver, so when uploading any file in Selenium you should never click on ‘Choose File’ button. Instead, just use sendkeys to select the file, WebElement El = driver.findElement(By.id("'fileUploadField'")); El.sendKeys("c:\\temp\\abc.txt"); – PassionateCoder Nov 13 '18 at 19:46
  • It doesn't work for me for these sites. Could you please show a working example? Maybe for this site: ec.europa.eu/cefdigital/DSS/webapp-demo/validation ? – plaidshirt Nov 14 '18 at 07:17
1

Try this one,

webElement.sendKeys(System.getProperty("user.dir") + "file path");

Here,

  • webElement is your element identified for file upload. Please make sure that the input element is visible.
  • try to specify file path as relative path of the content that we want to upload.

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.

Manju
  • 21
  • 5
  • For which element should I send the file? Could you please show an example? Maybe for this site: https://ec.europa.eu/cefdigital/DSS/webapp-demo/validation ? – plaidshirt Nov 12 '18 at 06:34
0

Use Selenium directly:

driver().findElement(By.id("ContentPlaceHolder1_Uploader1__Insert")).sendKeys(pathToFile);

With this you can avoid to use the system dependend dialog.

  • Yes, I tried it already, but it is not working in case of this field, as I mentioned in my question. – plaidshirt Oct 19 '18 at 07:34
  • Then I didn't get your question, what is wrong with the AutoIt code? Did you escape the AutoIT reserved characters? You can also use `Send($CmdLine[1])` – Thomas Raffelsieper Oct 19 '18 at 09:49
  • You may also try to use plain Javascript from within Selenium: `driver.executeScript("document.querySelector('input#myId').files = new Array('"+pathToFile+"')";` – Thomas Raffelsieper Oct 19 '18 at 09:56
  • AutoIT is working, but needs browser focus, not suitable to execute it in headless mode or simply without focus on browser. – plaidshirt Oct 19 '18 at 11:19
  • Could you please show a working example? Maybe for this site: ec.europa.eu/cefdigital/DSS/webapp-demo/validation ? – plaidshirt Nov 12 '18 at 06:37