7

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')
2
  • please show exact code you used to upload image Commented Jun 15, 2015 at 11:44
  • 1
    Please try to apply solution proposed here stackoverflow.com/questions/15049182/… Commented Jun 15, 2015 at 12:35

3 Answers 3

5

Solved with this:

driver.execute_script("document.getElementById('uploadImage'‌​).style.visibility='‌​visible'")
Sign up to request clarification or add additional context in comments.

Comments

3

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 for you.

Comments

0

I myself ran into this problem where my 'input' element looked like while using Cucumber tests with Selenium:

<input type="file" autocomplete="off" tabindex="-1" style="display: none;">

This is how I solved it:

JavascriptExecutor jse =(JavascriptExecutor) StepDef.driver;
        jse.executeScript("document.querySelector('" + elementSelector +"').style.display='block';");

        util.elementXpathIsVisible(elementXpath);
        StepDef.driver.findElement(By.xpath(elementXpath)).sendKeys(filePath);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.