1

I'm trying to upload a file to a website using python and selenium. I tried following methods

driver = webdriver.Firefox()
driver.get("ip-address")

method 1:

test = driver.find_element_by_id("selectedFile")
test.send_keys('/Users/knightfox/Desktop/file.txt')

method 2:

test = driver.find_element_by_xpath("//input[@type='file']")
test.send_keys('/Users/knightfox/Desktop/file.txt')

method 3:

test = driver.find_element_by_css_selector('input[type="file"]')
test.send_keys(r'/Users/knightfox/Desktop/file.txt')

But I get following errors when execute.

Traceback (most recent call last):
  File "/home/knightfox/Desktop/bell/sel.py", line 18, in <module>
    test.send_keys(r/Users/knightfox/Desktop/file.txt)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
    'value': keys_to_typing(value)})
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="selectedFile" name="file" type="file"> is not reachable by keyboard

I'm using Python 2.7. Following is the html page.

enter image description here

Update:

Following code by Ian solved the issue.

field = driver.find_element_by_id("selectedFile")
driver.execute_script("arguments[0].style.display = 'block';", field)
field = driver.find_element_by_id("selectedFile")
field.send_keys('/Users/knightfox/Desktop/file.txt')
6
  • Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. Commented Mar 14, 2018 at 10:19
  • problem seems to be reaching the keyboard stackoverflow.com/questions/48542737/… github.com/mozilla/geckodriver/issues/1184 The error “Element is not reachable by keyboard” means exactly what it says. The element can’t be reached using the keyboard, which means you can’t physically interact with it. Perhaps it’s hidden? Commented Mar 14, 2018 at 10:27
  • selenium webdriver upload file Commented Mar 14, 2018 at 10:29
  • I dont know if its hidden. I could see the code though. Any way we can find its hidden? Commented Mar 14, 2018 at 11:08
  • @DebanjanB I'm using a internal website for which I dont have access to html code. I'm using mozilla to inspect element. I want to show the field thats been used. I have uploaded the code and error and only for html I used screenshot. Commented Mar 14, 2018 at 11:11

1 Answer 1

3

The file field is hidden by its style="display: none;". Before you can interact with it, you need to make it visible.

field = driver.find_element_by_id("selectedFile")
driver.execute_script("arguments[0].style.display = 'block';", field)
field.send_keys('/Users/knightfox/Desktop/file.txt')
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks Ian. Now I see a Browse button on the page but still upload fails with same error. selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="selectedFile" name="file" type="file"> is not reachable by keyboard
@Knightfox You’ll have to be more specific than “still upload fails.” What’s happening? What is your script doing next? What is the error stacktrace?
I modified your the code and following works fine . field = driver.find_element_by_id("selectedFile") driver.execute_script("arguments[0].style.display = 'block';", field) field = driver.find_element_by_id("selectedFile") field.send_keys('/Users/knightfox/Desktop/file.txt')
Thanks for your help. Much appreciated
Please append this to your question as an update, then add the exception stacktrace.

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.