Prerequisites: Selenium Python basics, Browser Automation using Selenium
Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python.
Selenium can be used to automate the web browser. In this article, we will learn how to sign in a Google account and join a meeting through Google meet with a camera and microphone turned off using selenium.
Installation:
The selenium module can be installed using the below command:
pip install selenium
Step-by-step Approach:
Step 1: Import modules
Python3
# import required modules from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options import time |
Step 2: Create an instance of Chrome browser with some required prerequisite Chrome Options().
Python3
# creating chrome instance opt = Options() opt.add_argument( '--disable-blink-features=AutomationControlled' ) opt.add_argument( '--start-maximized' ) 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" : 0 , "profile.default_content_setting_values.notifications" : 1 }) driver = webdriver.Chrome(options = opt) |
Step 3: Go to Google meet using this command
Python3
# go to google meet |
Step 4: Calling the below function will turn off the camera and the microphone. In this function, turnOffMicCam(), we find the microphone & camera buttons by their xpaths using a function in selenium named find_element_by_xpath().
Python3
# explicit function to turn off mic and cam def turnOffMicCam(): # turn off Microphone time.sleep( 2 ) driver.find_element(By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/div[8]/div[3]/div/div/div[2]/div/div[1]/div[1]/div[1]/div/div[4]/div[1]/div/div/div' ).click() driver.implicitly_wait( 3000 ) # turn off camera time.sleep( 1 ) driver.find_element(By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/div[8]/div[3]/div/div/div[2]/div/div[1]/div[1]/div[1]/div/div[4]/div[2]/div/div' ).click() driver.implicitly_wait( 3000 ) |
Step 5: Below function would help us join the meeting at Google meet. In this function, joinNow(), we find the join now button by its css selector using a function in selenium named find_element_by_css_selector() and click it to join the meeting! (we do the same in AskToJoin() function).
Python3
def AskToJoin(): # Ask to Join meet time.sleep( 5 ) driver.implicitly_wait( 2000 ) driver.find_element(By.CSS_SELECTOR, 'div.uArJ5e.UQuaGc.Y5sE8d.uyXBBb.xKiqt' ).click() # Ask to join and join now buttons have same xpaths |
Step 6: Below is the function that would log in the Google account using the credentials provided. In this function, Glogin(mail,pw), we find the email text box by its id using a function in selenium named find_element_by_id() and type in the email using send_keys() & click next button again using its id and find password text box using its xpath, type in the password using send_keys & again click login using its id
Python3
def Glogin(mail_address, password): # Login Page driver.get( # input Gmail driver.find_element(By.XPATH, "identifierId" ).send_keys(mail_address) driver.find_element(By. ID , "identifierNext" ).click() driver.implicitly_wait( 10 ) # input Password driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input' ).send_keys(password) driver.implicitly_wait( 10 ) driver.find_element(By. ID , "passwordNext" ).click() driver.implicitly_wait( 10 ) # go to google home page driver.implicitly_wait( 100 ) |
Below is the complete program based on the above step-wise approach:
Python3
# import required modules from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options import time def Glogin(mail_address, password): # Login Page driver.get( # input Gmail driver.find_element(By. ID , "identifierId" ).send_keys(mail_address) driver.find_element(By. ID , "identifierNext" ).click() driver.implicitly_wait( 10 ) # input Password driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input' ).send_keys(password) driver.implicitly_wait( 10 ) driver.find_element(By. ID , "passwordNext" ).click() driver.implicitly_wait( 10 ) # go to google home page driver.implicitly_wait( 100 ) def turnOffMicCam(): # turn off Microphone time.sleep( 2 ) driver.find_element(By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/div[8]/div[3]/div/div/div[2]/div/div[1]/div[1]/div[1]/div/div[4]/div[1]/div/div/div' ).click() driver.implicitly_wait( 3000 ) # turn off camera time.sleep( 1 ) driver.find_element(By.XPATH, '//*[@id="yDmH0d"]/c-wiz/div/div/div[8]/div[3]/div/div/div[2]/div/div[1]/div[1]/div[1]/div/div[4]/div[2]/div/div' ).click() driver.implicitly_wait( 3000 ) def joinNow(): # Join meet print ( 1 ) time.sleep( 5 ) driver.implicitly_wait( 2000 ) driver.find_element(By.CSS_SELECTOR, 'div.uArJ5e.UQuaGc.Y5sE8d.uyXBBb.xKiqt' ).click() print ( 1 ) def AskToJoin(): # Ask to Join meet time.sleep( 5 ) driver.implicitly_wait( 2000 ) driver.find_element(By.CSS_SELECTOR, 'div.uArJ5e.UQuaGc.Y5sE8d.uyXBBb.xKiqt' ).click() # Ask to join and join now buttons have same xpaths # assign email id and password mail_address = 'emaild@gmail.com' password = 'neveropen' # create chrome instance opt = Options() opt.add_argument( '--disable-blink-features=AutomationControlled' ) opt.add_argument( '--start-maximized' ) 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" : 0 , "profile.default_content_setting_values.notifications" : 1 }) driver = webdriver.Chrome(options = opt) # login to Google account Glogin(mail_address, password) # go to google meet turnOffMicCam() # AskToJoin() joinNow() |
Output: