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/project.py

This cron job will run every minute.

Leave A Reply

Your email address will not be published. Required fields are marked *

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