0

I'm unable to upload and save an image.

The following is the piece of code that facilitates file upload using the UI. At first, only the upload button is visible and once an image is selected from the uploader the save button is visible.

 <td colspan="1" class="actions">
                <input type="button" class="button" name="logo" id="logo-f-upload" value="Upload Image"/>
                <input type="file" id="logo-upload" accept="image/*" name="logo-upload" style="display:none" />
                <input type="submit" value="click to save" name="submit_logo_now" class="main submit_dm logo-save-focused" style="display: none"/>
                </br>
</td>

I tried driver.find_element_by_id("logo-f-upload").send_keys(os.getcwd()+"/image_test.png")

and also

driver.find_element_by_id("logo-upload").send_keys(os.getcwd()+"/image_test.png")

but it doesn't throw any error at this stage but on the next where it says "element is not visible ..." ie. the save button. On the UI, when simulating this, the file uploader doesn't open but the file_upload button value changes to the path of the image and then breaks.

SiKing
  • 10,003
  • 10
  • 39
  • 90
L P
  • 1,776
  • 5
  • 25
  • 46
  • Have you looked at [this](http://stackoverflow.com/questions/14592853/how-to-upload-a-file-in-selenium-with-no-text-box?rq=1)? – dilbert May 25 '14 at 10:49
  • I'm using Linux and the input type is not MULTI so I don't think that may be the solution. – L P May 25 '14 at 10:54
  • So the problem is that 'logo-upload' is not visible on the screen while logo-f-upload is, although logo-upload is the input of type = file. Any idea what I can do now? – L P May 26 '14 at 13:14
  • What does `dir(driver.find_element_by_id("logo-upload"))` say? Maybe there's something special about input tags. – dilbert May 26 '14 at 13:17
  • Do you need to put a return character in the `send_keys`? – dilbert May 26 '14 at 13:26
  • It can't find the element logo-upload in the first place so that won't be the problem. – L P May 26 '14 at 13:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54453/discussion-between-l-p-and-dilbert). – L P May 26 '14 at 13:33

1 Answers1

1

I believe this is the answer which is merely a JS hack: So the problem is that the logo-upload input has style set to display:none and due to this, selenium can't find the element. The solution that works for me is using javascript to set the style to display:block, after which using send_keys(img_path) works fine.

dr.execute_script("document.getElementById('logo-upload').setAttribute('Style','display:block')")
L P
  • 1,776
  • 5
  • 25
  • 46