question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

🐛 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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
Goravedcommented, Jun 7, 2021

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

0reactions
diemolcommented, Jun 18, 2021

If you have any questions or need help, join us in the IRC/Slack channel where the community can help you as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - During uploading of existing file: selenium.common ...
During uploading of existing file : selenium.common.exceptions. ... InvalidArgumentException: Message: invalid argument: File not found ...
Read more >
How To Solve InvalidArgumentException In Selenium - YouTube
If you are starting selenium then you will end up getting InvalidArgumentException In Selenium and you can just fix by adding http or...
Read more >
selenium.common.exceptions — Selenium 4.7 documentation
Occurs if the given session id is not in the list of active sessions, meaning the session either does not exist or that...
Read more >
selenium.common.exceptions.invalidargumentexception ...
...implies that the url passed as an argument to get() was an argument was invalid. I was able to reproduce the same Traceback...
Read more >
[Code example]-Uploading a file in Selenium
I want to upload two image (.png) files .I used action keys and send keys to upload the file.It is working fine for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found