Set-Up
I'm using Python + Selenium to upload an image to the back-end of a system.
The upload button to which I send_keys() has the following HTML,
<div id="uploadifive-FileNameUpload" class="uploadifive-button" style="height:
18px; line-height: 18px; overflow: hidden; position: relative; text-align: center;
width: 50px;">
Upload
<input id="FileNameUpload" name="FileNameUpload" data-editor="#FileName"
data-url="http://also-inc.com/upload/uploadfile" data-path="~/UserFiles/Products/Images/"
data-maxsize="10240" data-extensions="*.jpg;*.jpeg;*.png;*.gif;*.bmp;"
data-thumbnailwidth="128" data-thumbnailheight="128"
data-thumbnailpath="/UserFiles/Products/Images/Preview/" data-uniquename="True"
data-preview="/Content/uploadify/noimage.jpg" data-isnew="true"
data-auth="D2C14774E29BBB87D2F34719884CFC5C6370502B067D5FC55D0C40A5EE6B1646ED4C77C9C0180D607052FF52653BA981732417A24C3F7547903649C4D64491C184E1C60D7756608784B4B3E806417E77750D87BABD9CDDCB6294EA62DE884EC7B3A4416558405874ED1C0259CD4430990BA83FC0"
data-session="f1txsiyxglqb3ma1dr45awrf" class="file-uploader hide-input" style="display: none;"
type="file">
<input style="font-size: 18px; opacity: 0; position: absolute; right: -3px; top: -3px; z-index: 999;" type="file">
</div>
Note that the button has two input with type="file".
Code Used
upload_button = el_id('uploadifive-FileNameUpload').find_element_by_xpath('input[2]')
upload_button.send_keys(path_to_my_image)
where el_id() = browser.find_element_by_id() and path_to_my_image ends with .jpeg.
Problem
The code used successfully uploads the image via the second input. The image, however, is saved without the .jpeg extension. Thus the image appears broken in the back-end; i.e. image_name instead of image_name.jpeg.
I think the image is saved without the extension because the second input does not allow for it.
Tries
- adding an attribute to second
input
The first input has the following attribute data-extensions="*.jpg;*.jpeg;*.png;*.gif;*.bmp;". I added this attribute to the second input,
upload_button = el_id('uploadifive-FileNameUpload').find_element_by_xpath('input[2]')
browser.execute_script("arguments[0].setAttribute('data-extensions','*.jpg;*.jpeg;*.png;*.gif;*.bmp;')", upload_button)
upload_button = el_id('uploadifive-FileNameUpload').find_element_by_xpath('input[2]')
upload_button.send_keys(path_to_my_image)
This added the attribute, but the uploaded image was still saved without .jpeg extension.
- changing
classandstyleattribute of firstinput
The first input has class="file-uploader hide-input" and style="display: none;", which I set to class="file-uploader" and style="display: block;" via,
upload_button = el_id('uploadifive-FileNameUpload').find_element_by_xpath('input[1]')
browser.execute_script("arguments[0].setAttribute('class','file-uploader hide-input')", upload_button)
upload_button = el_id('uploadifive-FileNameUpload').find_element_by_xpath('input[1]')
browser.execute_script("arguments[0].setAttribute('style','display: block')", upload_button)
upload_button = el_id('uploadifive-FileNameUpload').find_element_by_xpath('input[1]')
upload_button.send_keys(path_to_my_image)
but then the image doesn't upload. The upload button changes to a native OS file picker 'Select file' button.
How do I solve this?
Sidenote
I don't understand why this button has two input. I'm a beginner, so forgive me if I'm wrong, but intuitively I think a button just needs 1 input?