我最近升级了Lubuntu22.04,它希望从snap存储库中安装一些东西。火狐就是其中之一。目前,我正在使用Selenium 4.1.3、Python3.10和Firefox 99.0.1和最新的geckodriverv31.0
我已经使用这个python3代码进行了一段时间的测试,但现在它完全无法启动。
首先,它没能找到个人资料,所以我强行在里面放了些东西:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
options = Options()
options.add_argument("-profile /path2temp/") # create profile
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting",
False)
options.set_preference("browser.download.dir", "./data_export")
options.set_preference(
"browser.helperApps.neverAsk.saveToDisk",
"application/vnd.google-earth.kml+xml,application/x-zip-compressed,application/gpx+xml,text/csv"
)
options.set_preference("devtools.debugger.remote-enabled", True)
options.set_preference("devtools.debugger.prompt-connection", False)
browser = webdriver.Firefox(options=options, executable_path=r"/usr/bin/geckodriver")
url = 'https://cnn.com'
browser.get(url)如果firefox已经打开,它就无法与它通信。通常在过去,它只会打开一个新的选项卡并开始工作。但现在我发现了一个错误:
Firefox已经在运行,但没有响应。要使用Firefox,首先必须关闭现有的Firefox进程,重新启动设备,或者使用不同的配置文件。
如果我让它启动应用程序,那么在很多时间之后,它会出现以下错误(注意,/ path 2 time /是到达它拥有权限的目录的真正路径)。
1651528082918 geckodriver
INFO Listening on 127.0.0.1:54985 1651528083062 mozrunner::runner
INFO Running command: "/snap/bin/firefox" "--marionette" "-profile /path2temp/" "--remote-debugging-port" "47927" "-- remote-allow-hosts" "localhost" "-no-remote"
ATTENTION: default value of option mesa_glthread overridden by environment.
ATTENTION: default value of option mesa_glthread overridden by environment.
ATTENTION: default value of option mesa_glthread overridden by environment.
ATTENTION: default value of option mesa_glthread overridden by environment.
DevTools listening on ws://localhost:47927/devtools/browser/19a59834-6a4b-4d75-902c-06c36704d50e
Exiting due to channel error.
Exiting due to channel error.
Exiting due to channel error.
Exiting due to channel error.
Exiting due to channel error.我能做些什么来解决这个问题吗?
编辑:我至少能够通过将它传递给位于/home/username/snap/firefox/common/.mozilla/firefox/wnrrbapq.default-release文件结构中的当前用户配置文件,使它在启动firefox时工作。
但这并不是一个理想的行为,因为每次我都要关闭浏览器进行测试。
发布于 2022-05-14 06:14:35
Firefox版本不能写入/tmp位置。推荐的解决方法是将TMPDIR环境变量设置为geckodriver和firefox都可以写入的位置,例如$HOME/tmp。
发布于 2022-10-25 02:05:30
一种解决方案是根本不设置配置文件,而是为firefox和geckodriver定义文件路径。
因为Snap喜欢做一致的更新,我发现它有助于使我的文件路径动态。下面是一些示例代码:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
from subprocess import getoutput
options = Options()
options.binary_location = getoutput("find /snap/firefox -name firefox").split("\n")[-1]
driver = webdriver.Firefox(service =
Service(executable_path = getoutput("find /snap/firefox -name geckodriver").split("\n")[-1]),
options = options)
url = 'https://cnn.com'
driver.get(url)您可以使用getoutput获取/snap/firefox中以1长字符串表示的文件列表。根据\n分割它们并获取[-1] (最后)记录将为您提供该文件的最新版本。
这可能是你不想总是最新的版本,所以你总是可以选择列表上的第一个记录,如果你需要一致性。虽然这是很好的,Snap将更新geckodriver和Firefox在一起,所以他们应该永远一起工作。
https://stackoverflow.com/questions/72092807
复制相似问题