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
tmpfolder exists directly on the root so the path to it is/tmp?/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 thetempfilemodule.assert os.path.exists("/tmp/my_test_file")does not throw an error. I also tried using a subfolder in/tmpto no avail. Firefox is installed troughsnapthough if that makes a difference.