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?

3
  • 1
    Avoid using input and use a different name for your variable as that could be the cause. Can you share the webpage url? Commented Mar 13, 2019 at 7:09
  • 1
    This text box is most likely looking for a filename. Did you try giving full path with filename instead of 'hi'? Commented Mar 13, 2019 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 Commented Mar 13, 2019 at 19:44

2 Answers 2

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")
Sign up to request clarification or add additional context in comments.

5 Comments

Why is the second code block not suggested method? What's wrong in it?
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
Readability? re-usability? It's an <input> element and send_keys() will be followed by click() or submit(). Of coase the element will become stale
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.
@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.
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")

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.