Unrecognized file type in selenium python
<p>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</p>

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

<p>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</p>

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

<p>So the file type is unrecognized. Here is what I am using to upload the file:</p>

<pre><code>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')
</code></pre>

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p>Python + selenium also gives you the option of uploading the file directly.</p>

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

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

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

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

<p>Hope this helps!</p>

<p>More info about this for instance here: <a href="http://stackoverflow.com/questions/8665072/how-to-upload-file-picture-with-selenium-python">How to upload file ( picture ) with selenium, python</a></p>

python unit-testing selenium file-upload