I have a code that works perfectly in Visual Studio Code and I wanted to run in crontab. Since it has the Selenium module, I didn't managed to run the code without errors.
Code to run in crontab (by ipaleka)
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Firefox()
options = webdriver.FirefoxOptions()
options.add_argument("--headless")
driver.get("https://www.xe.com/currencyconverter/convert/?Amount=1&From=ISK&To=EUR")
loop = True
while loop: # This line is to avoid inconsistency
try:
#exchange_rate = driver.find_element_by_xpath('//*[@class="converterresult-toAmount"]').text
exchange_rate = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[2]/section/div[2]/div/main/div/div[2]/div[1]/p[2]/span').text
loop = False
except NoSuchElementException:
time.sleep(2)
driver.close()
print("Exchange rate updated: 0.00%s" % (exchange_rate))
Error
Traceback (most recent call last):
File "/home/<user>/Documentos/check.py", line 1, in <module>
from selenium import webdriver
ModuleNotFoundError: No module named 'selenium'
I've tried several ways to put in the crontab:
- Define Display first, then open firefox and perform the script as Luis said.
DISPLAY=:0
* * * * firefox
* * * * /usr/bin/env /usr/bin/python3 /home/<user>/Documentos/check.py >> /home/<user>/Documentos/log.txt
- Define all the vars and then perform the script as Jessica Chambers and Watty62 did.
SHELL=/bin/bash
PATH=/usr/local/bin/:/usr/bin:/usr/sbin:/usr/local/bin/geckodriver
* * * * * DISPLAY=:0 /usr/bin/python3 /home/<user>/Documentos/check.py > /home/<user>/Documentos/log.txt 2>&1
- Put in a single line display and path with anaconda as euh said.
* * * * * export DISPLAY=:0 && export PATH=$PATH:/usr/local/bin && /home/<user>/anaconda3/bin/python3 /home/<user>/Documentos/check.py >> /home/<user>/Documentos/log.txt
Even with PyVirtualDisplay and Xvfb didn't work as Muhammad Yasirroni posted it.
Create a bash script with all the variables and run that script in crontab as Amit Ghosh said. (Log was empty, so maybe I didn't run it)
Bash script (cinkselenium.sh)
export DISPLAY=:0
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
nohup /usr/bin/python3 -u /home/<user>/Documentos/check.py > /home/<user>/Documentos/log.txt &y
Crontab
* * * * * /root/apps/bridge/cinkselenium.sh
I have some points clear:
- Always put absolute paths (not only in the files).
- You have to export PATH to let cron reach gecko drivers.
- You need to specify the Display.
I'm running this on Ubuntu 22.04.
Any help is welcomed.