I have a Python script using Selenium and geckodriver, which runs correctly when executed from the console. However I can't get it running using cron!
This is my system:
Ubuntu 20.04.6 LTS
Python 3.8.10
Mozilla Firefox 113.0.2
geckodriver 0.33.0
This is a python script
from selenium import webdriver
from selenium.webdriver import FirefoxOptions
opts = FirefoxOptions()
opts.add_argument("--headless")
driver=webdriver.Firefox(options=opts)
As evident from this output, I use a python virtual environment and run the script as root and it works
Here is the entry in crontab:
* * * * * cd /root/pye && /root/pye/e1/bin/python3 /root/pye/test_sel3.py command arg 2>&1 | logger -t mycmd
I use
grep 'mycmd' /var/log/syslog to read the log of cron.
And this is the error, when cron attempts to run the script (or just initialize the webdriver):
May 29 13:39:01 wikijs-master CRON[1924951]: (root) CMD (cd /root/pye && /root/pye/e1/bin/python3 /root/pye/test_sel3.py command arg 2>&1 | logger -t mycmd)
May 29 13:39:02 wikijs-master mycmd: Traceback (most recent call last):
May 29 13:39:02 wikijs-master mycmd: File "/root/pye/test_sel3.py", line 7, in <module>
May 29 13:39:02 wikijs-master mycmd: driver=webdriver.Firefox(options=opts)
May 29 13:39:02 wikijs-master mycmd: File "/root/pye/e1/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 201, in __init__
May 29 13:39:02 wikijs-master mycmd: super().__init__(command_executor=executor, options=options, keep_alive=True)
May 29 13:39:02 wikijs-master mycmd: File "/root/pye/e1/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 286, in __init__
May 29 13:39:02 wikijs-master mycmd: self.start_session(capabilities, browser_profile)
May 29 13:39:02 wikijs-master mycmd: File "/root/pye/e1/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 378, in start_session
May 29 13:39:02 wikijs-master mycmd: response = self.execute(Command.NEW_SESSION, parameters)
May 29 13:39:02 wikijs-master mycmd: File "/root/pye/e1/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 440, in execute
May 29 13:39:02 wikijs-master mycmd: self.error_handler.check_response(response)
May 29 13:39:02 wikijs-master mycmd: File "/root/pye/e1/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
May 29 13:39:02 wikijs-master mycmd: raise exception_class(message, screen, stacktrace)
May 29 13:39:02 wikijs-master mycmd: selenium.common.exceptions.SessionNotCreatedException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
The highlighted error can be found in the internet. There are also solution suggestions. I tried really ALL of them and nothing works. I tried passing explicitely the executable paths of Firefox and geckodriver. I checked the environment of cron and saw there is no difference from the environment of the root user. I use headless mode. I tried exporting the current path in the cron entry. I tried defining a display although I don't use any. I used full paths everywhere. And so on...
Here also the root user environment:
env report follows for user root
LESSOPEN=| /usr/bin/lesspipe %s
USER=root
SSH_CLIENT=<XXX>
XDG_SESSION_TYPE=tty
SHLVL=1
MOTD_SHOWN=pam
HOME=/root
OLDPWD=/root
SSH_TTY=/dev/pts/0
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
LOGNAME=root
_=/root/envtst.sh
XDG_SESSION_CLASS=user
TERM=xterm
XDG_SESSION_ID=2031
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
XDG_RUNTIME_DIR=/run/user/0
LANG=C.UTF-8
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=>
SHELL=/bin/bash
LESSCLOSE=/usr/bin/lesspipe %s %s
PWD=/root/pye
SSH_CONNECTION=<XXX>
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
env report for user root concluded
And these are some relevent paths:
(e1) root@wikijs-master:~/pye# whereis firefox
firefox: /snap/bin/firefox.geckodriver /snap/bin/firefox
(e1) root@wikijs-master:~/pye# whereis geckodriver
geckodriver: /snap/bin/geckodriver
(e1) root@wikijs-master:~/pye# which geckodriver
/snap/bin/geckodriver
(e1) root@wikijs-master:~/pye# which firefox
/snap/bin/firefox
(e1) root@wikijs-master:~/pye# find /snap/firefox -name firefox
/snap/firefox
/snap/firefox/2710/usr/lib/firefox
/snap/firefox/2710/usr/lib/firefox/firefox
/snap/firefox/2667/usr/lib/firefox
/snap/firefox/2667/usr/lib/firefox/firefox
(e1) root@wikijs-master:~/pye# find /snap/firefox -name geckodriver
/snap/firefox/2710/usr/lib/firefox/geckodriver
/snap/firefox/2667/usr/lib/firefox/geckodriver
Any help is appreciated!