johnmcdnz
Posts: 3
Joined: Fri Sep 23, 2016 8:22 am

starting Chromium with crontab and selenium for autonomous website

Mon Jul 22, 2019 2:28 am

I'm listing here a python program with code that I have working on a Pi3b that has the purpose of starting Chromium automatically after restart of a Pi and logging into a website. I've obscured a few details to keep my application private. The Pi has a monitor connected on the HDMI i/f.

I struggled for a couple of days to get crontab and selenium to do what I needed and had to use a number of threads from others to piece this together.

The main challenge was getting crontab working. The main issues are:
ensuring #PATH is correct for chromium and setting DISPLAY.
Note that using a line such as -> @reboot DISPLAY=:0 && etc (DID NOT WORK) I had to have DISPLAY on a separate line.

make sure to install selenium

program name is test1.py, this is in a user directory Selenium, but that name is just a coincidence and not relevant to the installation of selenium

The actual purpose of my code is to login to a website, so the # lines are related to this purpose and I've kept them here in case it helps others doing similar.

Please note that I do not describe myself in any way as having linux expertise so can't enter into any detailed discussion on the operation or further fault finding.

Code: Select all

print('test1 start')

import time
import datetime
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

print ('imports')

now = datetime.datetime.now()
now_string = now.strftime("%Y-%m-%d %H:%M:%S") + " : "
print(now_string, 'web demo start')


# start chrome with options in kiosk mode
chromeoptions = Options()
chromeoptions.add_argument("--disable-infobars")
chromeoptions.add_argument("--kiosk")
driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', 0, chromeoptions)

print ('sites')
driver.get('http://www.bbc.com')
time.sleep(10)
#driver.find_element_by_name("username").send_keys("Username")
#driver.find_element_by_name("password").send_keys("Password")
#button = driver.find_element_by_name('commit').send_keys(Keys.RETURN)

print ('test1 end')
crontab -e

Code: Select all

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games:/usr/lib/chromium-b$
DISPLAY=:0.0


@reboot python3 /home/pi/Selenium/test1.py > /home/pi/Selenium/log.txt 2>&1


Return to “Troubleshooting”