Selenium脚本从控制台工作,在CRON中不工作-Geckodriver错误 [英] Selenium script working from console, not working in CRON - Geckodriver error

查看:92
本文介绍了Selenium脚本从控制台工作,在CRON中不工作-Geckodriver错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have Selenium script running from SH file. It is working perfectly fine when I run sh file from console, but the same file ran from Cron job fails.

SH文件:

#!/bin/sh

export DISPLAY=:10
cd /home/user
python3 selenium.py > /home/user/selenium.log 2>&1

我所遇到的错误是众所周知的:

Error which I am getting is well known:

回溯(最近通话最近):文件"/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中引发child_exception_type(errno_num,err_msg)FileNotFoundError:[Errno 2]没有这样的文件或目录:'geckodriver'

Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 74, in start stdout=self.log_file, stderr=self.log_file) File "/usr/lib/python3.5/subprocess.py", line 947, in init restore_signals, start_new_session) File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver'

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

During handling of the above exception, another exception occurred:

回溯(最近一次通话最后一次):文件"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中.

Traceback (most recent call last): File "so_login.py", line 12, in setUp self.driver = webdriver.Firefox() File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/firefox/webdriver.py", line 142, in init self.service.start() File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 81, in start os.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

我在控制台中也存在此错误,但是我通过安装geckodriver并将其移至/usr/local/bin来解决了该问题,并且在控制台上可以正常工作,但是为什么在CRON上却无法正常工作呢?

I have this error in console too, but I solved it by installing geckodriver and moving it to /usr/local/bin and it is working fine from console, but why it is not working from CRON ?

推荐答案

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

Consider using pyvirtualdisplay to manage your window session for you

使用pip安装

$ pip install pyvirtualdisplay

然后在代码中添加以下内容:

Then add something like the following to your code:

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

这篇关于Selenium脚本从控制台工作,在CRON中不工作-Geckodriver错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆