我目前正在 aws lambda(无服务器)上开发一个网络抓取项目
技术人员:
python3
硒3.14
geckodriver-V0.29
firefox 80.0(en 模式无头)
这是我实现的代码(知道我事先配置了firefox和geckodriver的路径):
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
options = FirefoxOptions()
options.add_argument("--headless")
binary = FirefoxBinary("/tmp/bin/firefox/firefox")
webdriver.Firefox(options=options,executable_path="/tmp/bin/geckodriver", firefox_binary=binary)
这是我得到的错误:
Message: Process unexpectedly closed with status 255
: WebDriverException
Traceback (most recent call last):
File "/var/task/src/lambda_function.py", line 8, in lambda_handler
driver = WebDriverWrapper()
File "/var/task/src/webdriver_wrapper.py", line 116, in __init__
self._driver = webdriver.Firefox(options=options,executable_path="/tmp/bin/geckodriver", firefox_binary=binary)
File "/var/task/lib/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
keep_alive=True)
File "/var/task/lib/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/var/task/lib/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/var/task/lib/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/var/task/lib/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 255
我搜索了论坛和文章,但没有找到解决方案。有没有人遇到过这种问题,如果可能的话如何纠正?
这些天我也遇到了同样的问题,显然是因为 Firefox 可执行文件是直接从 docker 容器内的链接下载的,而该容器没有一组 Firefox 需要工作的库。因此,为了解决这个问题,我必须将以下行添加到 Dockerfile。
RUN apt-get update && apt-get install -y wget bzip2 libxtst6 libgtk-3-0 libx11-xcb-dev libdbus-glib-1-2 libxt6 libpci-dev && rm -rf /var/lib/apt/lists/*
为了测试缺少哪些库,我连接到 docker 容器并尝试按以下方式运行 Firefox:
firefox -headless
(迟到的答案,留给下一个试图解决问题的可怜灵魂)
你正在无头奔跑。这是在没有头的虚拟机或容器中运行吗?你是从 tar 安装 FF 的吗?
对我来说,这是因为我运行的实例没有安装桌面,因此缺少 Firefox 依赖的一些库。我已经从下载并解压安装了 Firefox。
为了证明这就是问题所在,我从存储库安装了 Firefox,然后运行我的解压实例。看到这项工作表明我只是缺少依赖库。
我还没有弄清楚要从 Repo 安装的最小库集。
编辑:我发现
gitpod
的 gecko
容器提供了丰富的信息。
https://github.com/gitpod-io/workspace-images/blob/master/gecko/Dockerfile
这是关于 Firefox 的问题。 尝试将 Firefox 更新到最新版本。 我认为 Firefox 80.0 与 geckodriver v0.29 不兼容
如果您的 ubuntu 没有安装 Firefox,请尝试以下操作:
sudo apt install firefox
它对我有用。