selenium: During uploading of existing file: selenium.common.exceptions.InvalidArgumentException

πŸ› Bug Report

Selenium is telling me that an actual file currently in the system can’t be found

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:

To Reproduce

def add_profile_picture(self, profile_picture):
        local_photo_path = download_url_photo(profile_picture)
        print(type(local_photo_path), local_photo_path)
        file_inputs = WebDriverWait(self.driver, 5).until(
            EC.presence_of_all_elements_located((
                By.XPATH,
                '//div/input[(@type="file")]'                
            ))
        )        
        print("Is filepath local_photo_path: ", os.path.isfile(local_photo_path))
        print("Is filepath os.path.abspath(local_photo_path): ", os.path.isfile(os.path.abspath(local_photo_path)))
        file_inputs[0].send_keys(os.path.abspath(local_photo_path))

The download_url_photo function is defined this way:

def download_url_photo(url, filename=None):
    final_path = None
    base_path = '/tmp/'
    use_custom_filename = filename is not None

    
    response = requests.get(url)
    if response.status_code == 200:
        raw_data = response.content
        parsed_url = urlparse(url)
        if use_custom_filename:
            final_path = filename
        else:
            final_path = base_path + path.basename(parsed_url.path)
        with open(final_path, 'wb') as new_file:
            new_file.write(raw_data)

    if final_path:
        delete_image_exif_data(final_path)

    return final_path

As you can see, when I print the file location, the filepath gives me a True value, but the following error occurs: when I try to send_keys() to the file_inputs[0], the file_inputs[0] does exist, I’ve already checked it in various debugging processes.

Is filepath local_photo_path:  True
Is filepath os.path.abspath(local_photo_path):  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: Detailed steps to reproduce the behavior:

Expected behavior

The image should be uploaded or at least Selenium should recognize it

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

Environment

OS: Ubuntu 20.04 Browser: Chrome Browser version: Chrome=89.0.4389.90 Browser Driver version: Language Bindings version: Python 3.7.7 selenium==4.0.0b3 I am running inside a Docker container (version: β€œ3.6”), also using rabbitmq, celery services (django-celery-beat==2.1.0) and Django==3.1

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

I have the same issue when using selenoid and selenium 4.0.0.a7+. Works fine with 4.0.0.a6.