简体   繁体   中英

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error invoking send_keys() using Selenium

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?

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")

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() using Selenium Webdriver through Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error while uploading file using Selenium Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() with urls read from text file with Selenium Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator error using find_element('username') Selenium Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT' error when switching frame in Selenium selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'cookie' while adding cookies using selenium webdriver selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string using waits and expected conditions Python selenium for write review google maps (selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator ) URL must be a string selenium - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string Selenium By.CSS_SELECTOR selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM