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')
Knightfox
  • 103
  • 1
  • 2
  • 8
  • Please read why a [screenshot of HTML or code or error is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace. – undetected Selenium Mar 14 '18 at 10:19
  • problem seems to be reaching the keyboard https://stackoverflow.com/questions/48542737/python-selenium-element-is-not-reachable-by-keyboard https://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?` – Samuel Muiruri Mar 14 '18 at 10:27
  • [selenium webdriver upload file](https://stackoverflow.com/q/18823139/9400024) – Abdullah Ahmed Ghaznavi Mar 14 '18 at 10:29
  • I dont know if its hidden. I could see the code though. Any way we can find its hidden? – Knightfox Mar 14 '18 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. – Knightfox Mar 14 '18 at 11:11
  • @Knightfox You did provide a screenshot of the _HTML_ which is expected in the form of text format. That would help us immensely for a quick analysis. – undetected Selenium Mar 14 '18 at 11:15

1 Answers1

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')
Ian Lesperance
  • 4,961
  • 1
  • 26
  • 28
  • Thanks Ian. Now I see a Browse button on the page but still upload fails with same error. selenium.common.exceptions.ElementNotInteractableException: Message: Element is not reachable by keyboard – Knightfox Mar 14 '18 at 14:04
  • @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? – Ian Lesperance Mar 14 '18 at 14:05
  • 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') – Knightfox Mar 14 '18 at 14:25
  • Thanks for your help. Much appreciated – Knightfox Mar 14 '18 at 14:26
  • Please append this to your question as an update, then add the exception stacktrace. – Ian Lesperance Mar 14 '18 at 14:28