Renew U-Pass Automatically (British Columbia)

I often forget to renew my U-Pass.  It’s the first day of the month, I’d tap my Compass Card and the machine displays “Insufficient Funds”.  Though I’ve paid for it in the beginning of the semester, U-Pass asks us to renew it every month.  What a pain.

The code below automates the process of renewing U-Pass for SFU.  To automate that process, I use Windows Task Scheduler so that it runs the code in the background when my computer is on and is connected to the Internet.

(For UBC and other universities change the “sfu” string to corresponding university abbreviation based on the Upass website.)

The web-scraping API being used here is Selenium.  Note that to have a simulated browser open, you need a webdriver.  (For Google Chrome click here).  If you don’t want a web browser open, use PhantomJS webkit.

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select

# get the path for Chrome Driver
dir = os.path.join(os.path.dirname("__file__"))
chrome_driver_path = dir + "\chromedriver.exe"

# Create a new Chrome session
driver = webdriver.Chrome(chrome_driver_path)

# Use this if you prefer no browser open 
# driver = webdriver.PhantomJS()

driver.implicitly_wait(30)
driver.maximize_window()

# Navigate to Upass-BC Homepage
driver.get("https://upassbc.translink.ca/")

# Get the University Dropbox
search = Select(driver.find_element_by_id("PsiId"))

# University name
search.select_by_value("sfu")
driver.find_element_by_id("goButton").click()

# Arrived at University Website
username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")

# Make sure forms are empty
username.clear()
password.clear()

# Input Username and Password
username.send_keys("type_your_username_here")
password.send_keys("type_your_password_here")
password.submit()

# Screenshot for debugging purpose under PhantomJS
# driver.save_screenshot('screen3.png')

driver.find_element_by_id("chk_1").click()
driver.find_element_by_id("requestButton").click()

Due to lack of demand and security issues, I’ve decided not to make any system-based applications out of it.  Feel free to simplify the automation process even further.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s