Python Selenium脚本从控制台工作,不在CRON中工作-Geckodriver错误

Python Selenium脚本从控制台工作,不在CRON中工作-Geckodriver错误,python,linux,selenium,cron,Python,Linux,Selenium,Cron,我有从SH文件运行的Selenium脚本。 当我从控制台运行sh文件时,它工作得非常好, 但是从Cron作业运行的相同文件失败 SH文件: #!/bin/sh export DISPLAY=:10 cd /home/user python3 selenium.py > /home/user/selenium.log 2>&1 我得到的错误是众所周知的: 回溯最近的调用上次:文件 /usr/local/lib/python3.5/dist-packages/selenium

我有从SH文件运行的Selenium脚本。 当我从控制台运行sh文件时,它工作得非常好, 但是从Cron作业运行的相同文件失败

SH文件:

#!/bin/sh

export DISPLAY=:10
cd /home/user
python3 selenium.py > /home/user/selenium.log 2>&1
我得到的错误是众所周知的:

回溯最近的调用上次:文件 /usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py, 第74行,开始 stdout=self.log_文件,stderr=self.log_文件/usr/lib/python3.5/subprocess.py,第947行,在init中 还原信号,启动新会话文件/usr/lib/python3.5/subprocess.py,第1551行,在执行子进程中 引发子项\u异常\u类型Errno\u num,err\u msg FileNotFoundError:[Errno 2]没有这样的文件或目录:“geckodriver”

在处理上述异常期间,发生了另一个异常:

回溯上次调用:文件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.basenameself.path,self.start\u error\u消息selenium.common.exceptions.WebDriverException:消息:“geckodriver” 可执行文件需要在路径中

我在控制台中也有这个错误,但我通过安装geckodriver并将其移动到/usr/local/bin解决了这个问题,并且它在控制台中运行良好,但为什么它不能在CRON中运行呢?

考虑使用它来为您管理窗口会话

用pip安装它

$ pip install pyvirtualdisplay
然后在代码中添加如下内容:

from pyvirtualdisplay import Display


def main():
    # Display creates a virtual frame buffer and manages it for you
    with Display(visible=False, size=(1200, 1500)):
        # Run the test of your code here

    # When your code is finished and exits the with block, the with
    # context manager cleans up the virtual display for you


if __name__ == "__main__":
    main()

如果您需要一个X-Windows/GUI会话,那么它将无法与crontab一起工作,因为crontab不知道要与哪个X-Windows会话关联。如何使它工作?我像这里描述的那样使用它,Cron假设不显示,所以没有帧缓冲区。因此,我无法看到它与Cron一起工作。你和詹金斯商量过日程安排了吗?@RamanSailopal肯定可以。将XVFB用作X的虚拟帧缓冲区