How to access hidden file using Selenium WebDriver python
I have HTML like this:
<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>
Clicking on the system boost dialog to select a file that I cannot access via webdriver. I tried send_keys () directly, but it throws ElementNotVisibleException. So how can I upload a 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')
+3
source to share
2 answers
Execute JavaScript to make the input element visible before interacting with it.
driver.execute_script("""document.querySelector("div.yourClassNameHere input[type=file]").style.display='block'""")
# Send the absolute file path of the file to the input element
input = browser.find_element(:xpath, "//input[@type='file']")
input.sendKeys(os.path.abspath("image.jpg"))
Make sure you replace the queries with ones that make sense to you.
+1
source to share