-1

I need to automate the file upload feature in a web console and i am using selenium and java for this. I tried multiple approaches but when i click on upload button and windows explorer gets opened, it stops there. doesn't select any file...and gives error that there is no file.. I tried in firefox and chrome both but i am not able to solve this problem.

Then i also tried AutoIt tool. I downloaded it and made a script. trying to compile my script i am getting this error:

Code I'm using:

WebDriver driver = new FirefoxDriver(); 
driver.get("localhost:8080/page"); 
WebElement selectUploadApk = driver.findElement(By.id("id of upload button"));
selectUploadApk.click();
WebElement file = driver.findElement(By.xpath("//input[@type='file']")); 
file .sendKeys("path of the file");

Error: Unable to execute upx.exe to compress stub file File not found Exception

Please help

Thanx in advance

Megha

Mateusz
  • 3,038
  • 4
  • 27
  • 41
Megha
  • 21
  • 1
  • 3
  • We can't help you with the code if we can't see it. – Mateusz May 10 '13 at 11:57
  • Hi Code which i am using looks like this WebDriver driver = new FirefoxDriver(); driver.get("http://localhost:8080/page"); WebElement selectUploadApk = driver.findElement(By.id("id of upload button")); selectUploadApk.click(); WebElement file = driver.findElement(By.xpath("//input[@type='file']")); file .sendKeys("path of the file"); – Megha May 10 '13 at 12:20
  • I guess you should not open the dialogue... is the `//input[@type='file']` present before you click? – Franz Ebner May 10 '13 at 12:28
  • That is done to select file in windows explorer...windows explorer gets opened..but it is not selecting my file..//input[@type='file'] is given to select file..and then thru sendkeys i am entering path..i dont know whether it is correct or not..but i tried multiple approaches..still nothing working – Megha May 10 '13 at 12:31

1 Answers1

3

You don't need to click on the field to open the dialogue box.

Opening the dialogue box is what is 'breaking' your test.

Just send the keys directly to the input element, as you are, and then click on which ever button is the 'upload' button.

  driver.findElement(By.xpath("//input[@type='file']")).sendKeys("/path/to/file");
  driver.findElement(By.id("id of upload button")).click();
Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
  • Hey thanx it is working now...atleast uploading the files..but not closing windows explorer popup.. Can anyone help me with that.. – Megha May 13 '13 at 06:31
  • So to clarify the explorer window opens when you click the `upload` button? So following what I put before, it sends the path to the `input` element and then clicks on `upload`. That begins the upload as expected BUT still opens the explorer window as well? – Mark Rowlands May 13 '13 at 08:16
  • Yes.. it will upload the file but doesn't close the windows explorer popup. which further creates problem in loop and doent go to the next file to attach and gives exception. I am trying javascript code so as to close that popup window..but its not working... – Megha May 13 '13 at 09:02
  • Hmm, that is unusual practice as far as I know. In all other cases for me, at least, the explorer window would only open if the user clicks on the `input` element or the `upload` element IF the path hasn't already been set. Could you provide some of the relevant HTML perhaps? Hopefully that'll give me a clue, thanks. – Mark Rowlands May 13 '13 at 09:10
  • You need a .perform() after your .click() http://stackoverflow.com/questions/16597588/how-to-handle-windows-file-upload-window-when-using-selenium – Ajax Jul 12 '13 at 10:11
  • The `Actions()` class isn't being used here so there's no need for the `.perform()`. – Mark Rowlands Jul 12 '13 at 10:39