CentOS中执行cron,调用selenium总是提示geckodriver路径不对

作者: loaf 分类: IT 发布时间: 2018-07-10 11:30

在CentOS中建立了一个Cron任务,但是运行总是不成功,然后发一封邮件,提示代码中调用的文件路径不存在。

Traceback (most recent call last):
File “/home/loaf/PycharmProjects/testssh/getweb.py”, line 7, in <module>
driver = webdriver.Firefox()
File “/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py”, line 160, in __init__
self.service.start()
File “/usr/lib/python2.7/site-packages/selenium/webdriver/common/service.py”, line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.

我用crontab -e看任务的内容如下:

*/20 * * * *  /usr/bin/python2.7 /home/loaf/PycharmProjects/testssh/getweb.py

在网上看到一个解释,是如果任务里使用了绝对路径来执行脚本,那么脚本代码里引用的其它脚本也得使用绝对路径,看来这就是原因了,所以将任务代码修改为

*/20 * * * * cd /home/loaf/PycharmProjects/testssh/ && python2.7 getweb.py

但是问题仍然存在,再用crontab -e进入,原来只有上面一行,现在加上

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/loaf/firefox
*/20 * * * * cd /home/loaf/PycharmProjects/testssh/ && python2.7 getweb.py

这样才解决了路径问题,但是又有了新的问题

Traceback (most recent call last):
File “getweb.py”, line 7, in <module>
driver = webdriver.Firefox()
File “/usr/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py”, line 170, in __init__
keep_alive=True)
File “/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 156, in __init__
self.start_session(capabilities, browser_profile)
File “/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 251, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File “/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py”, line 320, in execute
self.error_handler.check_response(response)
File “/usr/lib/python2.7/site-packages/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 1

看看geckodriver.log文件,里面显示

1531206001533 geckodriver INFO geckodriver 0.21.0
1531206001538 geckodriver INFO Listening on 127.0.0.1:38755
1531206002541 mozrunner::runner INFO Running command: “/home/loaf/firefox/firefox” “-marionette” “-foreground” “-no-remote” “-profile” “/tmp/rust_mozprofile.NVrprNCvujjv”
Error: no DISPLAY environment variable specified

看起来好像是因为没有显示环境,而我直接在终端是可以运行的,而且相类似的代码,我在Windows环境下也可以做计划任务,为什么在CentOS里就不行呢?这就有些奇怪了。在网上找到这个网页,好像提到了这种情况,尽管日志里的提示并不尽相同。按他所说,修改了getweb.py里的代码

from selenium import webdriver
from selenium.webdriver import FirefoxOptions

opts = FirefoxOptions()
opts.add_argument(“–headless”)
driver= webdriver.Firefox(firefox_options=opts)

driver.get(‘http://www.96369.net’)

这次终于成功了。