Ola, welcome to my first entry! :D

In this post, I will show you how to use the webdriver from Selenium, time and schedule libraries to create an auto-booking script for North Hill Badminton Court.

Let us start by importing the modules.

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
import schedule

If you haven’t install selenium and webdriver in your environment, typing pip install selenium and pip install webdriver-manager in your terminal will do the magic. If you haven’t install pip, download get-pip.py from the web and run it with python.

Next, run the code below to initialise Chrome Web Driver - a tool for automated testing of web apps.

browser = webdriver.Chrome(ChromeDriverManager().install())
#Put the link you want the webdriver to access in the function. In this case, it is NTU SRC booking website
browser.get('https://sso.wis.ntu.edu.sg/webexe88/owa/sso_login1.asp?t=1&p2=https://wis.ntu.edu.sg/pls/webexe88/srce_smain_s.Notice_O&extra=&pg=') 
time.sleep(2)

After using the .get() method to open the page, it is time for us to input our NTU username and password to log in.

user_name = 'INPUT_YOUR_NTU_USERNAME'
fill_name = browser.find_element_by_name('UserName')
fill_name.send_keys(user_name)

submit_name_button = browser.find_element_by_name('bOption')
submit_name_button.click()
time.sleep(2)

user_password = 'INPUT_YOUR_NTU_PASSWORD'
fill_password = browser.find_element_by_name('PIN')
fill_password.send_keys(user_password)

submit_password_button = browser.find_element_by_name('bOption')
submit_password_button.click()
time.sleep(3)

Here, you may be wondering. WHERE ARE UserName, bOption and PIN COMING FROM? Horror…. the Horror…. :O

If you go to NTU SRC log in website, then use the inspect function (for Windows user is CTRL+Shift+I, MAC user is Command+Option+I), you can see that the box where you should type your username will have the attribute of name='UserName'. So basically what our code does are:

  1. Find the element with name='UserName'
  2. Use the .send_keys() method to input your username.
  3. Find the element with name='bOption', which is the OK button
  4. Click the button
  5. Find the element with name='PIN'
  6. Use the .send_keys() method to input your password.
  7. Find the element with name='bOption', which is the OK button
  8. Click the button
  9. And you are logged in!

Next part of the code is to select the radio button for badminton court, select your preferred time slot, and book!

#Select Badminton 
badminton_radio = browser.find_element_by_xpath("//input[@value='1BB26']")
badminton_radio.click()
time.sleep(3)

#Select Slot
select_time = browser.find_element_by_xpath("//input[@value='1BB2BB0324-Mar-20225']")
select_time.click()
time.sleep(2)

#Click confirm booking button
confirm = browser.find_element_by_xpath("//input[@value='Confirm']")
confirm.click()
time.sleep(5)

Below are the code in Chrome’s DevTools, use inspect function to see the code.
Select Badminton (value=‘1BB26’) 1 Select Slot (value=‘1BB2BB0324-Mar-20225’) 2 Click confirm booking button (value=‘Confirm’) 3 To schedule the code for it to run automatically, you will need the schedule library to do the work. Code is as below:

#Note: Code will run too without this block of code. Try `python <filename>.py` in your terminal.

def job():
    # This is where you input the code that you want to implement at the specific timing.
    # Basically copy all the above code and paste here, except for the import libraries part.
    
schedule.every().day.at("00:00").do(job)

while True:
    schedule.run_pending()
    time.sleep(1) 

Lastly, in your terminal, run python <filename>.py and it is done!

Some fun stuffs that you can try yourself:

  1. Use nohup command in terminal so the <filename>.py will run even if the user logged out.
  2. Adjust the number in time.sleep() and see what happens, sometimes the code will work even if you comment out the line(s).
  3. Change the “00:00” in the schedule function to see how the code works in the next minute.

Update: 8 Feb 2023

Hey there! Just a quick update on running a Python script in the background. A few hours ago I happened to watch this youtube video about how to host a Telegram Bot on a server 24/7. We could use pythonanywhere to host, run and code Python in the cloud. It has both free and premium features. I thought this technique could also be applied to our Python Selenium project so do give it a try if you’re interested to run your script in cloud.

That’s all from me! Thank you for your time, hope you had a blast :)