1

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 ?

Marek Urbanowicz
  • 12,659
  • 16
  • 62
  • 87
  • If you need an X-Windows/GUI session then it won't work with crontab as crontab doesn't know which X-Windows session to associate itself with. – Raman Sailopal Aug 05 '17 at 10:32
  • 1
    how to make it work? I use it headless like described here https://medium.com/@griggheo/running-selenium-webdriver-tests-using-firefox-headless-mode-on-ubuntu-d32500bb6af2 – Marek Urbanowicz Aug 05 '17 at 10:56
  • Cron assumes not display and so no frame buffers. I therefore can't see it working with Cron. Have you looked into scheduling with Jenkins instead? – Raman Sailopal Aug 05 '17 at 13:13
  • @RamanSailopal sure it can work. Use XVFB as a virtual framebuffer for X – Corey Goldberg Dec 04 '17 at 20:59

1 Answers1

3

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()
Levi Noecker
  • 3,142
  • 1
  • 15
  • 30