首页 文章

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

提问于
浏览
0

我有从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行,在start 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]没有这样的文件或目录:'geckodriver'在处理上述异常期间,发生了另一个异常:Traceback(最近一次调用最后一次):文件“so_login.py”,第12行,在setUp 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'executa需要在PATH中 .

我在控制台中也有这个错误,但我通过安装geckodriver并将其移动到/ usr / local / bin来解决它,它在控制台上工作正常,但为什么它不能从CRON工作?

1 回答

  • 1

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

    用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()
    

相关问题