3

I'm trying to upload file using python selenium on a website whose file field in html looks like this:

 <div id="fallback" style="display: none;">
    <input id="upload-input" type="file" name="file" multiple="multiple">
    <div id="upload-progress" class="upload-progress"></div>
  </div>

I'm trying to make element visible via following code:

elem = driver.find_element_by_xpath("//input[@id='upload-input']")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem = driver.find_element_by_xpath("//input[@id='upload-input']")

After running the script, script stops without uploading the file and without throwing any error.

After using elem.is_displayed(), I've found the element is still not displayed even after running the above block of code.

1

1 Answer 1

12

The style attribute is on the wrapping <div>, but you're trying to remove it from the <input>:

container = driver.find_element_by_id("fallback")
driver.execute_script("arguments[0].style.display = 'block';", container)

input = driver.find_element_by_id("upload-input")
input.send_keys(path_to_file)

P.S. The use of the style attribute is an implementation detail of how an element is styled. In this case, you really only care about what the style is, so better to specifically set it to what you want rather than delete a particular way of implementing it. The former should be less brittle.

P.P.S. You probably don't need to use XPath for something as simple as a lookup by ID. (IDs should be unique across all elements; otherwise they're not really IDs.)

Sign up to request clarification or add additional context in comments.

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.