I use Selenium and Python to send requests to a website. I am making a file upload right now, but it seems that it is not working as I expect it to be.
Here is how to HTML looks like:
<div id="uploaders" class="uploaders" data-image-urls="" data-image-ids="" data-image-positions="" data-image-checksums=""><div id="uploader-container-0" class="uploader-container small empty" data-uploader-index="0" data-numbering="1" style="position: relative;">
<div id="file-picker-0" class="uploader-box small" style="position: relative; z-index: 1;">
<div class="thumb">
<div class="uploader-overlay">
<span class="photo-action edit-action">
<span class="mp-Icon-circle"><span class="mp-Icon mp-svg-edit photo-action-icon"></span></span>
</span>
<span class="remove photo-action">
<span class="mp-Icon-circle"><span class="mp-Icon mp-svg-delete photo-action-icon"></span></span>
</span>
</div>
</div>
<div class="content">
<div class="mp-svg-button-camera camera-logo centered"></div>
<div class="main-photo-subtext centered">
Hoofdfoto.
</div>
</div>
</div>
<input type="hidden" name="images.urls" value="">
<input type="hidden" name="images.ids" value="">
<input type="hidden" name="images.checksums" value="">
<div id="html5_1bdvv7bg0ptl15m6gmu1d4v16l04_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; overflow: hidden; z-index: 0;">
<input id="html5_1bdvv7bg0ptl15m6gmu1d4v16l04" type="file" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" multiple="" accept="image/jpeg,.jpg,.jpeg,image/bmp,.bmp,image/png,.png"></div>
As you can see in the bottom of the code there is a input type file element. But no form. The file upload on the site works as follows. You click on a (+) sign. Then I file choose dialog box opens, you choose the file. And then there comes another pop-up which asks with a button to click if you want accept something.
My question is how can I upload a file. Till yet I have failed to do this.
My code:
upload_photo_field_xpath = ".//*[@id='uploader-container-0']/div/input"
upload_photo_element = WebDriverWait(driver, 10).until(
lambda driver: driver.find_element_by_xpath(upload_photo_field_xpath))
image = os.path.join(os.getcwd(), 'images/' + 'img.jpg')
print(image)
upload_photo_element.send_keys(image)
upload_photo_element.submit()
Any thoughts on how to solve this problem?
Thanks.