Automate Google Classes with Selenium and Python

Welcome to our automation tutorial series. In previous tutorials, you learned about automating tasks with python and also learned about the Selenium library of python to automate web tasks. In this tutorial, we’re going to use the same Selenium library of python to automatically attend your google classes at the scheduled time.

The only purpose of this tutorial is education and learning.

Getting Started

Before diving into this tutorial, you guys should be familiar with creating a virtual environment and installing the required packages for this work.

Package Installations

The required packages for this project are:

Installing Selenium

This is the required package for automating browser tasks. You can install this into your system by using the command:

pip install selenium

Installing Keyword

These are the required packages for using your keyboard virtually for any automation task. You can install it into your system by using the command:

pip install keyboard

Schedule

This is the required python package for scheduling events. You can install it into your system by using the command:

pip install schedule

Algorithm

The steps for the task are as follows:

  1. First, you need to import the required packages as mentioned above.
  2. Then, you look into the task and what you’ve to do.
  3. The first and foremost work is to open the browser.
  4. Then you need to sign in with our google account and redirect to google classroom.
  5. After that, you’ll find the class you need to join and open it.
  6. Then you’ll navigate to the meet link in the top-right corner of that class to join the meet.
  7. Google meet webpage will appear and you’ll need to join the meet by turning off our mic and camera.
  8. You’ll wait for the class to end and then leave the meeting.
  9. At last, you’ll schedule the same job for every particular day (for this tutorial, you’ll schedule it for every Sunday.)

Diving into the code

Importing Libraries

At first, you’ll import all required libraries into our code

import schedule #Schedule library you installed earlier
from selenium import webdriver #Webdriver from selenium library
import time #time is a pre-installed library in python
import keyboard #keyboard library you installed earlier

from selenium.webdriver.common.keys import Keys #Keys from selenium library
from selenium.webdriver.chrome.options import Options #Options from selenium

Adding Variables

Two types of variables are added:

  • Webdriver Options: These are set of variables to allow or block different permissions like mic, camera, location, notifications, etc. to the browser.
opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
opt.add_experimental_option("prefs", { \
    "profile.default_content_setting_values.media_stream_mic": 1,
    "profile.default_content_setting_values.media_stream_camera": 1,
    "profile.default_content_setting_values.geolocation": 2,
    "profile.default_content_setting_values.notifications": 2
  })
  • Specific Variables: These are developer-assigned variables assigned to store the google account username and password and the URL to sign in with the google account.
url = 'https://accounts.google.com/signin/v2/identifier?service=classroom&passive=1209600&continue=https%3A%2F%2Fclassroom.google.com%2Fu%2F0%2Fh&followup=https%3A%2F%2Fclassroom.google.com%2Fu%2F0%2Fh&flowName=GlifWebSignIn&flowEntry=ServiceLogin'
gmail_username = '[email protected]'
gmail_password = 'YOUR_Password'

Defining Function

Now, you def the function to do automation job:

def job():

Get the web driver and open it with provided permissions values assigned in the variables above and provided driver location:

driver = webdriver.Chrome(options=opt, executable_path=r'C:\Users\DELL\chromedriver\chromedriver.exe')

Maximize browser window (This is optional and needs to be included only if you want to open the browser window in full screen.)

driver.maximize_window()

Open the URL assigned above to log in to your Google Account and redirect to google classroom.

driver.get(url)

Wait for the site to be loaded completely before moving to the next command.

driver.implicitly_wait(60)

Now selenium finds the input field to enter the email for the google account assigned above as gmail_username and enter the next button and wait for the next page to be loaded:

driver.find_element_by_id('identifierId').send_keys(gmail_username)
driver.find_element_by_id('identifierNext').click()
driver.implicitly_wait(60)

Now selenium finds the input field to enter the password for the google account assigned above as gmail_password and enter the next button and wait for next page to be loaded:

driver.find_element_by_name('password').send_keys(gmail_password)
driver.find_element_by_id('passwordNext').click()
driver.implicitly_wait(60)
time.sleep(10)

Find the xpath of class you want to join.

xpath is a unique path id of any HTML element and can be used to locate the particular element easily.

Once you’re on your google classroom on the main page, right-click on the class you want to join and click on inspect.

Click on inspect

Now you move to the highlighted line on the code tab opened and right-click. You’ll see a list of options, click on copy and select Copy XPath.

Copy XPath

now you paste the copied XPath to python code as pre-defined variables.

class_xpath = '//*[@id="yDmH0d"]/div[2]/div/div[5]/div/ol/li[1]/div[1]/div[3]/h2/a[1]/div[1]'

now open the class you want to join and get the XPath of meet link button on the top-right corner in a similar way and paste it as a new variable to the python code.

meet_xpath = '//*[@id="yDmH0d"]/div[2]/div[2]/div[4]/div[1]/div/div[2]/div[2]/div/span/a/div'

Now, you’ll make selenium click on these buttons by recognizing them with their xpaths.

driver.find_element_by_xpath('class_xpath').click()
time.sleep(5) //Wait for 5sec to let the page load
driver.find_element_by_xpath('meet_xpath').click()
time.sleep(5) //Wait for 5sec to let the page load

Now you’ll be redirected to the Google meet site with a similar interface.

Google Meet window

Now, you need to turn off its mic and camera, so you can use the tab key from your keyboard to navigate buttons and enter the key to click on it. This is where our keyboard library will come to use now.

keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("enter", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("enter", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("tab", do_press=True, do_release=True)
keyboard.send("enter", do_press=True, do_release=True)

Pressing the “Tab” key 5 times will navigate to the mic and then you press “enter” to turn off the mic. Then press “Tab” again move to the camera button and press “enter” to turn it off. Now again press the “Tab” button 4 times to move to the join button and send “enter”.

That’s it. You’re in you’ve successfully joined your google class. Now you stay in the class for the duration of class time and press “Ctrl + W” to leave the class after the completion of class time.

time.sleep(2700) #Wait for 45min==2700sec to let the class time finish
keyboard.press_and_release('ctrl+w')
print("Class Attended !!")

Scheduling Class

Now you’ve successfully created the function to attend the google class and you need to execute it on a particular day and time so you schedule the task for every Sunday at 2:33 PM.

#Outside the funtion
schedule.every().sunday.at("14:33").do(job)
schedule.run_pending()

Complete Code

Here is the complete and neat code for the task but you’ll need to change the login email and password and XPath values and schedule the day and time as per required.

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

opt = Options()
opt.add_argument("--disable-infobars")
opt.add_argument("start-maximized")
opt.add_argument("--disable-extensions")
# Pass the argument 1 to allow and 2 to block
opt.add_experimental_option("prefs", { \
    "profile.default_content_setting_values.media_stream_mic": 1,
    "profile.default_content_setting_values.media_stream_camera": 1,
    "profile.default_content_setting_values.geolocation": 2,
    "profile.default_content_setting_values.notifications": 2
  })

url = 'https://accounts.google.com/signin/v2/identifier?service=classroom&passive=1209600&continue=https%3A%2F%2Fclassroom.google.com%2Fu%2F0%2Fh&followup=https%3A%2F%2Fclassroom.google.com%2Fu%2F0%2Fh&flowName=GlifWebSignIn&flowEntry=ServiceLogin'
gmail_username = '[email protected]'
gmail_password = 'YOUR_Password'

def job():
    print("It's class time !!")
    driver = webdriver.Chrome(options=opt, executable_path=r'C:\Users\DELL\chromedriver\chromedriver.exe')
    driver.maximize_window()
    driver.get(url)

    driver.implicitly_wait(60)
    driver.find_element_by_id('identifierId').send_keys(gmail_username)
    driver.find_element_by_id('identifierNext').click()

    driver.implicitly_wait(60)
    driver.find_element_by_name('password').send_keys(gmail_password)
    driver.find_element_by_id('passwordNext').click()
    driver.implicitly_wait(60)
    time.sleep(10)
    class_xpath = '//*[@id="yDmH0d"]/div[2]/div/div[5]/div/ol/li[1]/div[1]/div[3]/h2/a[1]/div[1]'
    meet_xpath = '//*[@id="yDmH0d"]/div[2]/div[2]/div[4]/div[1]/div/div[2]/div[2]/div/span/a/div'
    driver.find_element_by_xpath('class_xpath').click()
    time.sleep(5) //Wait for 5sec to let the page load
    driver.find_element_by_xpath('meet_xpath').click()
    time.sleep(5) //Wait for 5sec to let the page load
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("enter", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("enter", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("tab", do_press=True, do_release=True)
    keyboard.send("enter", do_press=True, do_release=True)
    time.sleep(2700)
    keyboard.press_and_release('ctrl+w')
    print("1st Class Attended !!")

schedule.every().sunday.at("14:33").do(job)

schedule.run_pending()

Conclusion

You’ll need to let this script run and it’ll automatically join your google class as explained above. This tutorial is just for information and learning and explains to you an example of automating browser tasks using python and selenium library, so we expect that you do not use it for any kind of immoral activities.

Posted by Awebisam

I am Awebisam, a tech and economics enthusiast with native roots in Python and JS. Apart from those, TS and Golang are also my type of things.
I work with Django, FastAPI, Vue and Nestjs. Also slightly familiar with Flask, Express, and Svelte.

~ Awebisam, Aashish Bhandari

Leave a Reply

Your email address will not be published. Required fields are marked *