Selenium script working from console, not working in 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 file:
#!/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:
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:
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.
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 ?
Consider using pyvirtualdisplay to manage your window session for you
Install it with 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()
链接地址: http://www.djcxy.com/p/62760.html
上一篇: Python Selenium入门错误