1

Currently I have this input

<label class="bold grey-rectangle" data-uploading="Uploading" data-completed="Upload completed">
  <span>+ Add certificate</span>
  <input type="file" data-validation-allowing="jpg, jpeg, pdf" data-validation="mime" name="qualification2">
  <div class="spinner hidden">
  <div class="spinner-inner"></div>
  </div>
</label>

and I want to send a file to the same, so I'm running the following codes:

self.driver.execute_script("document.getElementsByName('qualification2')[0].style.display='block';")
self.driver.find_element_by_name("qualification2").send_keys("certificate.jpeg")

but I'm getting the following error

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

How, effectively, I can do an upload with python and selenium?

4
  • Are you sure there are no other elements with the same name value? Commented Jan 13, 2016 at 23:10
  • I'm sure, this name is unique! Commented Jan 13, 2016 at 23:19
  • Thanks, could you please inspect the element in the browser developer tools and see how exactly it is made invisible?.. Commented Jan 13, 2016 at 23:20
  • I think that some attributes are not fixed to this input. See the code now: <input class="hide" type="text" data-validation-error-msg-container="#qualification-error-dialog" data-validation-error-msg="You must upload a qualification" name="qualification" required="" data-validation="required"> CSS: .hide { display: none !important; Commented Jan 13, 2016 at 23:30

1 Answer 1

2

You should try removing the hide class to make the element visible:

elm = self.driver.find_element_by_name("qualification2")
self.driver.execute_script('arguments[0].removeAttribute("class");', elm)

elm.send_keys("/absolute/path/to/certificate.jpeg")

Used the link you've provided, tested it:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://your/site")

elm = driver.find_element_by_name("qualification2")
driver.execute_script('arguments[0].removeAttribute("class");', elm)

elm.send_keys("/Users/user/Downloads/test.jpg")

Produced:

enter image description here

Note that in Firefox you would also have to reset the margin-left style property to 0 to make the element really visible:

driver.execute_script('arguments[0].removeAttribute("class"); arguments[0].style["margin-left"] = 0;', elm)
Sign up to request clarification or add additional context in comments.

11 Comments

Worked fine, but when I try load my file, is sent only a string: See: screencast.com/t/zbMAz2swr You can help me with this?
@Rafael interesting, according to the screenshot, it does not look like a file input, but a regular input instead..is this a public site tat you can share a link to? Thanks.
Is not a public site, How I can send URL to you via PM?
@Rafael we should trigger a suggestion to continue the discussion in chat.
Got it. remove my site
|

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.