1

I am not able to execute the below attached python script via crontab. When I run script directly from CLI it works just fine, I see just a quick action how browser is opened, everything is setup and then browser closes. But when run in crontab, nothing happens. Crontab entry:

* * * * * /full/path/to/script.py

I also tried :

* * * * * /usr/bib/python /full/path/to/script.py

SCRIPT:

#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.support.ui import Select

browser = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver")
#open website
browser.get("http:website.php")
# find element in dropdown
Select(browser.find_element_by_name("ma")).select_by_value("817")
# confirm by clicking button
element = browser.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td[3]/input")
element.click()

# select dropdown 
Select(browser.find_element_by_name("status")).select_by_value("11")

#confirm dropdown
submit = browser.find_element_by_xpath("/html/body/form/table[3]/tbody/tr/td/input")
submit.click()

browser.close()

/var/log/syslog:

May  4 11:34:01 pavol-Vostro-15-3568 CRON[11685]: (root) CMD (/full/path/to/script.py)
Palino1611
  • 39
  • 2
  • You should take a look at this: https://stackoverflow.com/questions/2135478/how-to-simulate-the-environment-cron-executes-a-script-with Once you have the same cron environment in your shell it is easier to catch the error – FxIII May 04 '20 at 09:41

1 Answers1

2

I faced a problem similar to yours. Your job might be failing because it requires an X session since you're trying to open a web browser. You should put export DISPLAY=:0; after the schedule in your cronjob, as in

* * * * * export DISPLAY=:0; /usr/bib/python /full/path/to/script.py

If this doesn't work, you could try replacing :0 with the output of echo $DISPLAY in a graphical terminal.

AtilioA
  • 419
  • 2
  • 6
  • 19