我想使用Selenium Firefox驱动程序上传一个文件。该文件是动态生成的,不需要持久保存,因此我想将其放在/tmp/
下。
虽然从我的home
目录中选择文件工作得很好:
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")
对位于/tmp/my_test_file
的文件执行相同操作,如下所示:
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")
会导致以下粗略的错误消息:
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
这很奇怪,因为文件确实存在
我找不到方法让它工作。为什么从/tmp/
选择文件时会有区别?
访问权限都是-rw-rw-r--
倍,通过单击“浏览...”按钮手动选择文件,并选择文件也工作得很好。
**编辑:**我正在运行Kubuntu 22.04.1的全新安装
1条答案
按热度按时间zi8p0yeb1#
在Ubuntu 22.04上也遇到了同样的问题,安装firefox作为.deb(不是一个快照)帮助了我。
1.删除Firefox快照:
sudo snap remove firefox
1.将(Ubuntu)Mozilla团队PPA添加到您的软件源列表中:
sudo add-apt-repository ppa:mozillateam/ppa
1.更改Firefox软件包优先级:
echo ' Package: * Pin: release o=LP-PPA-mozillateam Pin-Priority: 1001 ' | sudo tee /etc/apt/preferences.d/mozilla-firefox
1.设置firefox自动更新:
echo 'Unattended-Upgrade::Allowed-Origins:: "LP-PPA-mozillateam:${distro_codename}";' | sudo tee /etc/apt/apt.conf.d/51unattended-upgrades-firefox
1.通过apt安装Firefox
sudo apt install firefox
使用了以下说明:https://www.omgubuntu.co.uk/2022/04/how-to-install-firefox-deb-apt-ubuntu-22-04