Using Crontab and Selenium

I have been setting up a python script using Selenium to automate some web tasks. One of the tasks I wanted to automate completely. I ran into a few problems trying to run the python script in crontab. The python script would run fine in the terminal but not using crontab. After a lot of Googling and StackOverFlow research I came up with a solution to the problem.

There are two issues:

  1. Crontab uses very limited PATH variables.
  2. Crontab doesn’t use a display or terminal to run code. Everything runs in the background.

Initially, I was getting this error: “Message: ‘geckodriver’ executable needs to be in PATH.”

The fix for that is to add the PATH to the browser driver to the cronjob.

The second issue I ran into was the cronjob not having a display to use.

The fix for that is to add a Display to the cronjob command.

I set this up in Ubuntu 19.04 using crontab -e

Here is the end result:

* * * * * export DISPLAY=:0 && export PATH=$PATH:/usr/local/bin && /usr/bin/python3 /home/username/Documents/Project/

This cron job will run every minute.

5 comments on “Using Crontab and Selenium”

  1. Blake Reply

    Thank you for this, tried so many solutions but this was the one that finally worked. I think the export PATH was what did it for me when compared to other solutions.

  2. Mohammed Reply

    It finally works. Thank you so much! I’ve been trying to figuring this out!

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.