Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access hidden file upload field with Selenium WebDriver python

I have such HTML:

<a id="buttonToUpload" class="btn-pink medium" href="#">
    <span class="icon-arrow-right">upload photo</span>
</a>
<form id="form1" enctype="multipart/form-data">
    <input id="uploadImage" type="file" accept="image/png, image/gif, image/jpeg, image/jpg" style="visibility: hidden">
</form>

Pressing on raise system dialog for choosing file, which I can't access via webdriver. I tried send_keys() directly to but it raises ElementNotVisibleException. So how can I upload photo there? Actual code:

driver = webdriver.Firefox()
driver.get('http://www........')
upload_input = driver.find_element_by_id('uploadImage')
upload_input.send_keys(os.getcwd()+'/image.jpg')
like image 761
wasd Avatar asked Jun 15 '15 11:06

wasd


1 Answers

Solved with this:

driver.execute_script("document.getElementById('uploadImage'‌​).style.visibility='‌​visible'")
like image 173
wasd Avatar answered Oct 05 '22 07:10

wasd