selenium - 使用 Chrome 的 Python cron 作业未在 AWS EC2 中运行
问题描述
一个月左右,我一直在使用 EC2 实例每天使用 cron 运行 python 脚本。该脚本使用硒。直到今天,当我的脚本没有运行时,一切都正常运行。
我试图手动运行它,但它也不起作用。错误消息说
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.
NoSuchElementException: Message: no such element: Unable to locate element:
{"method":"cssselector","selector":"#ctl00_ctl00_moteurRapideOffre_
ctl01_EngineCriteriaCollection_Contract > option:nth-child(5)"}
(Session info: headless chrome=90.0.4430.85)
但是,相同的脚本在我的计算机上运行良好(即在我的 Macbook 上,而不是在 AWS EC2 上)。
由于问题似乎来自 Chrome,我在 AWS EC2 上使用以下命令将其卸载:
sudo yum remove google-chrome-stable
然后我使用重新安装它:
curl https://intoli.com/install-google-chrome.sh | bash
sudo mv /usr/bin/google-chrome-stable /usr/bin/google-chrome
google-chrome --version && which google-chrome
如果我尝试在 EC2 上使用 Chrome 运行 Chrome /usr/bin/google-chrome
,它不起作用并显示以下错误消息:
ERROR:browser_main_loop.cc(1386)] Unable to open X display.
我不知道它以前是否有效,因为我从未以这种方式使用过它。但这似乎是一个问题。
我在网上看到它可能来自没有屏幕的事实,我应该使用一个名为xvfb的包。我尝试使用以下代码安装它:
sudo yum install xorg-x11-server-Xvfb
我猜该软件包已正确安装,但效果不佳。
总而言之,我认为我在 python 代码中的问题与 Google Chrome 无法正常工作有关,这可能与 xvfb 有关。但我完全不确定,这正是我到目前为止所尝试的。
请你帮助我好吗 ?谢谢!
解决方案
推荐阅读
- c++ - 使用带有派生类作为参数的 std::function 和 std:bind 的事件管理器的 C++ 回调
- c - 一般来说,我应该/是否应该在 C 中转换一个 void 指针?
- php - 如何在多列中设置文本?
- c# - 调用 ClassA.Method() 作为 ClassB.Method() 但只影响 ClassA 值,两个类都派生自同一个 BaseClass
- django - 如何将 django 表单错误作为文本而不是 html?
- dask - GKE 上的 dask-kubernetes 零工人
- blender - 搅拌机 2.8 UV 展开扭曲
- php - PHP - 多个 Api 调用 - 响应时间超过 15 秒?
- python - 如何返回不在列表中的键?
- javascript - 如何在 code.org applab 中创建碰撞