首页 > 解决方案 > 使用 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 有关。但我完全不确定,这正是我到目前为止所尝试的。

请你帮助我好吗 ?谢谢!

标签: seleniumgoogle-chromeamazon-ec2selenium-chromedriverxvfb

解决方案


推荐阅读