1

I'm new to Selenium Automated Testing and I'm just trying to do a simple task by typing in "hi" in the text box on a web page.

My code looks like this:

input = driver.find_element(By.XPATH, "//input[@type='file']")
input.send_keys('hi')

But when I run the code I received this error:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : hi

Any ideas on how to fix this?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
vyou
  • 31
  • 1
  • 1
  • 2
  • 1
    Avoid using _input_ and use a different name for your variable as that could be the cause. Can you share the webpage url? – QHarr Mar 13 '19 at 07:09
  • 1
    This text box is most likely looking for a filename. Did you try giving full path with filename instead of 'hi'? – anish Mar 13 '19 at 11:06
  • You succeeded in typing 'hi' into the text box. The problem is that the textbox is an `INPUT` that is expecting a filename... thus the error. Did you read the error message? `File not found : hi` – JeffC Mar 13 '19 at 19:44

2 Answers2

1

You first need to import "By"

from selenium.webdriver.common.by import By
input=driver.find_element(By.XPATH, '//input[@type="file"]')
input.send_keys("hi")

You can also write it in (although not suggested method to do )

from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//input[@type="file"]').send_keys("hi")
YoDO
  • 93
  • 1
  • 7
  • Why is the second code block _not suggested method_? What's wrong in it? – undetected Selenium Mar 13 '19 at 08:51
  • Readability and re-usability decreases . What if we want to use the same element to be cleared after entering text? we will again write the "driver.find_element" **instead store the webelement in a variable and then perform operations on it** – YoDO Mar 13 '19 at 08:55
  • _Readability_? _re-usability_? It's an `` element and `send_keys()` will be followed by `click()` or `submit()`. Of coase the element will become **stale** – undetected Selenium Mar 13 '19 at 08:58
  • No, you would refactor the code to separate out the `.find_element()` and store its return in variable and then do the later actions using the variable. There's nothing wrong with a one-liner here that gets refactored later. In your first case, you are storing a result in a variable that is not needed. – JeffC Mar 13 '19 at 19:41
  • @DebanjanB Not all `.send_keys()` are followed by `.click()` or `.submit()`. There could easily be other actions after like YoDO suggested, e.g. `.clear()`, if you are testing invalid inputs, etc. – JeffC Mar 13 '19 at 19:43
1

This error message...

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found

...implies that the WebDriver instance was unable to find a file through the character sequence you have sent through send_keys().

The relevant HTML DOM would have helped us to debug your issue in a better way. Still it is clear from the Locator Strategy which you have used, that the expected content must be of type as file. Additionally it is possible, there is a JavaScript involved which checks the contents passed to the element if at all the contents refers to a valid file.


Solution

You need to pass a valid file as an argument with send_keys() as follows:

driver.find_element(By.XPATH, "//input[@type='file']").send_keys("/path/to/filename.extension")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352