IMPORTANT NOTE: I am using selenium==4.0.0b3
During the process of uploading an existing while using selenium-webdriver, I did the test to find is the file actually exists to corroborate that I was actually uploading something and being consistent with it, my testing code:
print("Is filepath", os.path.isfile(local_photo_path))
file_inputs[0].send_keys(os.path.abspath(local_photo_path))
The filepath gives me a True value, but the following error occurs:
Is filepath True
ERROR [_CatchWebDriverError][204] invalid argument: File not found : /tmp/69f9dddf-6f69-490e-9ed3-d9a379e151af.jpg
(Session info: chrome=89.0.4389.90)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : /tmp/69f9dddf-6f69-490e-9ed3-d9a379e151af.jpg
(Session info: chrome=89.0.4389.90)
Stacktrace:
#0 0x56282084f2b9 <unknown>
Reading the Selenium documentation, it seems I am doing it correctly, this is the actual selenium function:
def send_keys(self, *value) -> None:
"""Simulates typing into the element.
:Args:
- value - A string for typing, or setting form fields. For setting
file inputs, this could be a local file path.
Use this to send simple key events or to fill out form fields::
form_textfield = driver.find_element(By.NAME, 'username')
form_textfield.send_keys("admin")
This can also be used to set file inputs.
::
file_input = driver.find_element(By.NAME, 'profilePic')
file_input.send_keys("path/to/profilepic.gif")
# Generally it's better to wrap the file path in one of the methods
# in os.path to return the actual path to support cross OS testing.
# file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))
"""
# transfer file to another machine only if remote driver is used
# the same behaviour as for java binding
print('This is send keys')
if self.parent._is_remote:
local_files = list(map(lambda keys_to_send:
self.parent.file_detector.is_local_file(str(keys_to_send)),
''.join(map(str, value)).split('\n')))
if None not in local_files:
remote_files = []
for file in local_files:
remote_files.append(self._upload(file))
value = '\n'.join(remote_files)
self._execute(Command.SEND_KEYS_TO_ELEMENT,
{'text': "".join(keys_to_typing(value)),
'value': keys_to_typing(value)})