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
CodePudding user response:
Faced the same problem on Ubuntu 22.04, installing firefox as .deb (Not a Snap) helped me.
- Remove the Firefox Snap:
sudo snap remove firefox
- Add the (Ubuntu) Mozilla team PPA to your list of software sources:
sudo add-apt-repository ppa:mozillateam/ppa
- Change Firefox package priority:
echo ' Package: * Pin: release o=LP-PPA-mozillateam Pin-Priority: 1001 ' | sudo tee /etc/apt/preferences.d/mozilla-firefox
- Setup firefox auto-updates:
echo 'Unattended-Upgrade::Allowed-Origins:: "LP-PPA-mozillateam:${distro_codename}";' | sudo tee /etc/apt/apt.conf.d/51unattended-upgrades-firefox
- 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