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.
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')

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?