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)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.