willer2k
willer2k

Reputation: 546

Having trouble navigating file explorer with python

I am building an automated browser with selenium and it is working flawlessly! (thank you selenium (: ) But I am having trouble uploading a file. One of the steps I need to execute is to upload a file.

The code I use to upload, and seems like it works for many people, is:

file_input = driver.find_element_by_id('ImageUploadButton')
file_input.send_keys('C:\\image.jpg')

Also tried:

driver.find_element_by_id('ImageUploadButton').click()
driver.find_element_by_css_selector('input[type="file"]').clear()
driver.find_element_by_css_selector('input[type="file"]').send_keys('C:\\image.jpg')

This seems to work for a lot of people, but for me, it just opens the file explorer for me to pick the file I want to upload, and that is it. No error message, just continues to execute the code.

Is anyone aware of maybe another module that I can use to navigate the file explorer and submit the file?

Or am I using selenium inappropriately?

----------- edit ---------------

Added DIV from website:

    <div id="FileInputWrapper" class="file-input-wrapper">
    <input id="FileUploadInput" type="hidden" name="file">
    <button id="ImageUploadButton" class="button-update-cancel short file-upload-button" type="button" style="position: relative; z-index: 1;"> Select Images</button>
    </div>
<input type="hidden" name="images">
<div id="html5_1auv7g94u187l1qdq108d1ue5qve3_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 518px; left: 0px; width: 155px; height: 45px; overflow: hidden; z-index: 0;">
<input id="html5_1auv7g94u187l1qdq108d1ue5qve3" type="file" accept="image/jpeg,image/png,image/gif,image/bmp" multiple="" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;">
</div>

Upvotes: 1

Views: 188

Answers (1)

Andersson
Andersson

Reputation: 52665

Seems that you use wrong locator to upload file. You should handle input element, not button:

file_input = driver.find_element_by_xpath('//input[@type="file"]')
file_input.send_keys('C:\\image.jpg')

Upvotes: 2

Related Questions