【发布时间】:2018-01-13 04:46:14
【问题描述】:
我有从 SH 文件运行的 Selenium 脚本。 当我从控制台运行 sh 文件时,它工作得很好, 但从 Cron 作业运行的相同文件失败。
SH 文件:
#!/bin/sh
export DISPLAY=:10
cd /home/user
python3 selenium.py > /home/user/selenium.log 2>&1
我得到的错误是众所周知的:
Traceback(最近一次调用最后一次):文件 "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", 第 74 行,开始 stdout=self.log_file, stderr=self.log_file) 文件“/usr/lib/python3.5/subprocess.py”,第 947 行,在 init restore_signals,start_new_session)文件“/usr/lib/python3.5/subprocess.py”,第 1551 行,在 _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'
在处理上述异常的过程中,又发生了一个异常:
Traceback(最近一次调用最后一次):文件“so_login.py”,第 12 行,在 设置 self.driver = webdriver.Firefox() 文件“/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py”, 第 142 行,在 init 中 self.service.start() 文件“/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py”, 第 81 行,开始 os.path.basename(self.path),self.start_error_message)selenium.common.exceptions.WebDriverException:消息:'geckodriver' 可执行文件需要在 PATH 中。
我在控制台也有这个错误,但我通过安装 geckodriver 并将其移动到 /usr/local/bin 解决了它,它在控制台上工作正常,但为什么它不能从 CRON 工作?
【问题讨论】:
-
如果您需要一个 X-Windows/GUI 会话,那么它将无法与 crontab 一起使用,因为 crontab 不知道与哪个 X-Windows 会话关联。
-
如何让它工作?我像这里描述的那样无头使用它medium.com/@griggheo/…
-
Cron 假定不显示,因此没有帧缓冲区。因此,我看不到它与 Cron 一起工作。您是否考虑过使用 Jenkins 进行调度?
-
@RamanSailopal 确定它可以工作。使用 XVFB 作为 X 的虚拟帧缓冲区
标签: python linux selenium cron