如何使用Selenium WebDriver Python访问隐藏文件上传字段 [英] How to access hidden file upload field with Selenium WebDriver python

查看:103
本文介绍了如何使用Selenium WebDriver Python访问隐藏文件上传字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的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>

按下引发系统对话框以选择文件,我无法通过webdriver访问该文件. 我尝试了send_keys()直接,但它引发ElementNotVisibleException.那么,我该如何上传照片呢? 实际代码:

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')

推荐答案

解决了这个问题:

driver.execute_script("document.getElementById('uploadImage'‌​).style.visibility='‌​visible'")

这篇关于如何使用Selenium WebDriver Python访问隐藏文件上传字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆