1

So I am testing a web client which communicates with an engine that does something to a text file that I upload. So basically, I choose a file to upload, once this file is uploaded i can press start and the engine does its thing and returns a result. I am trying to test the front end using selenium in python. This web client accepts zip or txt files. The developer of this web client made it such that when a file type other than a zip or text is uploaded it will give an error like so

File type "audio/wav" is not supported: Must be one of "text/plain", "application/zip", "application/zip-compressed", "application/x-zip-compressed".  

In this case, I tried to upload a wav audio file. When I try to manually upload a zip file, it works as expected. However, when I try this same process using the same file in selenium, it no longer recognize the file type and gives me this error

File type "" is not supported: Must be one of "text/plain", "application/zip", "application/zip-compressed", "application/x-zip-compressed". 

So the file type is unrecognized. Here is what I am using to upload the file:

choose = self.driver.find_element_by_id("chooseButton")
time.sleep(1)
#clicks to open upload window
choose.click()
time.sleep(1)
#ZIp file with other zips
pyautogui.typewrite("C:\\Transcriber\\Framework\\test\\audio\\Nested.zip")
time.sleep(1)
pyautogui.press('enter')

I am using pyautogui to manipulate the upload window that pops up when I click the upload button, so it's as though I am automating my keyboard. The time.sleep is just to ensure that enough time is provided between actions such that an action ends before the next one starts.

My zip is just an ordinary zip file. When I run it in selenium it gave me the above error. Does anyone know what the problem could be? Is this a python issue? Thanks in advance.

Edit: This issue only occurs when I try to upload a zip file, if I replace the zip file in my test case with a txt file, then it works fine.

Edit2: After my test case finishes, if I leave the browser opened, the error would still occur even I I try to upload manually. So this seems to happen only in the browser instance spawned by selenium. Otherwise, if I open a fresh browser on my own, uploading a zip works fine.

Ted
  • 365
  • 1
  • 7
  • 16
  • When you choose the file to upload, do you choose it with the mouse or do you type in the path? What happens if you type in the path just like pyautogui does and press enter? Perhaps the file is name is not picked up and it is trying to pass an empty filename to the uploader, hence the 'File type "" is not supported' error. – tarikki Sep 12 '16 at 16:18
  • So, in my code above, I used pyautogui to type it in, since when the upload windows pops up selenium no longer has control, so I need to "manually" enter the keys. I can see that the path is complete when I monitor the test since there is a 1 second pause to make sure that it is done printing before pressing enter. I have tried the exact path in a manual test and it works. Of course in my manual test I had to remove the double backslashes. – Ted Sep 12 '16 at 18:20

1 Answers1

0

Python + selenium also gives you the option of uploading the file directly.

I am not sure whether this is true in your case, because I don't have the complete html for the "choose" element.

In my case I had an element of input[type=file] and this worked:

driver.find_element_by_css_selector('input[type="file"]').send_keys(path+filename)

Maybe you have to click an ok/submit button after that, depending on your situation.

Hope this helps!

More info about this for instance here: How to upload file ( picture ) with selenium, python

Community
  • 1
  • 1
Chai
  • 1,796
  • 2
  • 18
  • 31
  • `
    Drag or choose WAV/JSON files here.
    ` Not sure if this helps, but I have tried your method and it didn't seem to work in my case
    – Ted Sep 13 '16 at 14:15
  • was there any difference in behaviour? Or did it give the same message ? Thanks for this info. It doesn't help me as much as I'd like, so I am hoping someone else maybe knows more! However: Have you tried my suggestion using the element #files ? – Chai Sep 13 '16 at 14:20
  • I have tried sendKeys before but I didn't know that I had to click it afterwards as well, but that being said, nothing happens in my case even if I press it afterwards. The upload windows just pops up still. That's the main reason I had to resort to using pyautogui to "manually" type in the path and press enter. The strange thing is that (maybe I should mention it in my question as well), it seems to work fine when I upload a regular text file, so in that same test case, if I change the zip file to a txt file, I get no issues – Ted Sep 13 '16 at 15:22
  • Ah, that seems interesting! Could it be that the zip is a lot bigger, takes more time somehow, and your script runs a little too fast then? Have you tried it with a really small .zip file? – Chai Sep 14 '16 at 06:17
  • No, none of the zips work. The developer of this web client actually changed it now so that it accepts anything that ends with .zip, but this issue will continue to bother me until the end of time if I don't find the answer... – Ted Sep 14 '16 at 18:06
  • Hmm..strange... let me know if you solve it, before it starts bothering me as well :) – Chai Sep 15 '16 at 07:35