Travis Lunglhofer
Travis Lunglhofer

Reputation: 11

Selenium - Python script works from CLI, but errors when launching with cron

Ill start with the fact that I am a total beginner at Python scripts and programming in general. I want to automate a Raspberry Pi to boot up, open Chromium in full screen, go to a web page, and log in all automatically. Basically so non technical people can just turn it on and bang, there is it no keyboard or anything needed, just a display. I've been working on this for about a week and have learned a ton, but I have hit a wall and cant get past it.

Running on a Ras Pi 4+, with Raspbian 10

Installed Selenium and Chromedriver

I have the Selenium script done in Python:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-fullscreen')
chrome_options.add_argument('user-data-dir=/home/pi/Documents/website_login/Chromium_user_data')
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")

driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=chrome_options)

driver.get("https://www.mywebsite.com")

driver.implicitly_wait(10)

#driver.find_element_by_id('rcc-confirm-button').click()  #comment out because after run once the cookies banner gets saves in user settings

driver.find_element_by_link_text('Log in').click()

delay = 5

driver.find_element_by_id('ddlsubsciribers').send_keys('agency')
driver.find_element_by_id('memberfname').send_keys('user')
driver.find_element_by_id('memberpwd').send_keys('password')
driver.find_element_by_id('login').click()

It works fine when I run it from the terminal. Opens chromium, goes to the page, does the Selenium magic, logs in and is exactly what I want.

I then try to get it to launch on startup with crontab -e on the Pi using this command:

@reboot sleep 20; /usr/bin/python3 /home/pi/Documents/website_login/iar_login.py > /home/pi/Documents/website_login/iar_errorlog.err >2&1

Nothing happens when I reboot and I get the following error messages in the log file:

Traceback (most recent call last):
  File "/home/pi/Documents/website_login/iar_login.py", line 16, in <module>
    driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=chrome_options)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

I have searched and read everything I can on the errors. I think it may be a permissions error of some type, but I am out of ideas to try.

Upvotes: 0

Views: 461

Answers (1)

Travis Lunglhofer
Travis Lunglhofer

Reputation: 11

Fixed. adding 'export DISPLAY=:0' in the crontab file fixed it. Selenium was trying to execute but it couldn't find a display.

Upvotes: 1

Related Questions