1

I want to upload a file using the selenium Firefox driver. The file is dynamically generated and does not need to persist so I would like to place it under /tmp/.

While selecting files from my home directory works perfectly fine:

from selenium import webdriver
from pathlib import Path

driver = webdriver.Firefox()
driver.get("https://viljamis.com/filetest/")
Path(f"{Path.home()}/my_test_file").touch()
driver.find_element('xpath', '//input[@type="file"]').send_keys(f"{Path.home()}/my_test_file")

doing the same with a file located at /tmp/my_test_file like so:

from selenium import webdriver
from pathlib import Path

driver = webdriver.Firefox()
driver.get("https://viljamis.com/filetest/")
Path(f"/tmp/my_test_file").touch()
driver.find_element('xpath', '//input[@type="file"]').send_keys(f"/tmp/my_test_file")

results in the following crude error message:

Traceback (most recent call last):
  File "/home/username/Downloads/test.py", line 12, in <module>
    driver.find_element('xpath', '//input[@type="file"]').send_keys(f"/tmp/my_test_file")
  File "/home/username/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 233, in send_keys
    self._execute(
  File "/home/username/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webelement.py", line 410, in _execute
    return self._parent.execute(command, params)
  File "/home/username/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "/home/username/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: File not found: /tmp/my_test_file
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:182:5
InvalidArgumentError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:311:5
interaction.uploadFiles@chrome://remote/content/marionette/interaction.sys.mjs:537:13

which is strange because the file definitly does exist

I can't find a way to make it work. Why is there a difference when selecting files from /tmp/?

The access-rights are both times -rw-rw-r-- and manually selecting the file by clicking the "browse..." button and selecting the file also works perfectly fine.

Edit: I am running a fairly fresh installation of Kubuntu 22.04.1

3
  • Are you sure the tmp folder exists directly on the root so the path to it is /tmp ? Commented Dec 11, 2022 at 11:09
  • You did not name your OS, but it could be some special protection of temporary files. The /tmp/ folder is usually shared among all users and requires some setup for this to work properly. You could try to create a folder inside of /tmp/ and create your files there of use the tempfile module. Commented Dec 11, 2022 at 11:15
  • @Prophet Yes, i think so. Also assert os.path.exists("/tmp/my_test_file") does not throw an error. I also tried using a subfolder in /tmp to no avail. Firefox is installed trough snap though if that makes a difference. Commented Dec 11, 2022 at 11:18

1 Answer 1

1

Faced the same problem on Ubuntu 22.04, installing firefox as .deb (Not a Snap) helped me.

  1. Remove the Firefox Snap:

sudo snap remove firefox

  1. Add the (Ubuntu) Mozilla team PPA to your list of software sources:

sudo add-apt-repository ppa:mozillateam/ppa

  1. Change Firefox package priority:

echo ' Package: * Pin: release o=LP-PPA-mozillateam Pin-Priority: 1001 ' | sudo tee /etc/apt/preferences.d/mozilla-firefox

  1. Setup firefox auto-updates:

echo 'Unattended-Upgrade::Allowed-Origins:: "LP-PPA-mozillateam:${distro_codename}";' | sudo tee /etc/apt/apt.conf.d/51unattended-upgrades-firefox

  1. Install Firefox via apt

sudo apt install firefox

Used the following instruction: https://www.omgubuntu.co.uk/2022/04/how-to-install-firefox-deb-apt-ubuntu-22-04

Sign up to request clarification or add additional context in comments.

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.