Open In App

Automatically filling multiple responses into a Google Form with Selenium and Python

Prerequisite: 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 Tutorial covers all topics such as– WebDriver, WebElement, Unit Testing with selenium. 



The Task here is to fill multiple responses with the same Google form using selenium in python. Link to the Google form used in this example is given below:

Form link – Click Here



GOOGLE FORM

The form has five entries:

Name, Email & Phone Number has same class name quantumWizTextinputPaperinputInput and Address & Comments has same class name quantumWizTextinputPapertextareaInput. Data is in the form of a list.

The given program also uses count, reason being textboxes contain a list, whose class name is “quantumWizTextinputPaperinputInput” and textareaboxes contain a list, whose class name is “quantumWizTextinputPapertextareaInput” and when these two classes are added it results in a list. The data is also provided in a form of a list, thus count variable will be incremented with each data variable.

Example:

# values of data

[name, email, phone number, address, comments]
datas = [
   [‘Mary D Joiner’,’MaryDJoiner@gmail.com’,’4079025063′,’2474  McDonald Avenue,Maitland’,’NA’],
   [‘Karen B Johnson’,’KarenBJohnson@gmail.com’,’3153437575′,’2143  Oak Street,GRAND ISLE’,’NA’],
]

Approach

To achieve our required functionality given steps needs to followed in a perfect order:

Program:




# Import Module
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
 
# open Chrome
driver = webdriver.Chrome(
    'C:/Users/HP/Desktop/Drivers/chromedriver_win32/chromedriver.exe')
 
# Open URL
 
# wait for one second, until page gets fully loaded
time.sleep(1)
 
# Data
datas = [
    ['Mary D Joiner', 'MaryDJoiner@gmail.com', '4079025063',
        '2474  McDonald Avenue,Maitland', 'NA'],
    ['Karen B Johnson', 'KarenBJohnson@gmail.com',
        '3153437575', '2143  Oak Street,GRAND ISLE', 'NA'],
]
 
# Iterate through each data
for data in datas:
    # Initialize count is zero
    count = 0
 
    # contain input boxes
    textboxes = driver.find_elements_by_class_name(
        "quantumWizTextinputPaperinputInput")
 
    # contain textareas
    textareaboxes = driver.find_elements_by_class_name(
        "quantumWizTextinputPapertextareaInput")
 
    # Iterate through all input boxes
    for value in textboxes:
        # enter value
        value.send_keys(data[count])
        # increment count value
        count += 1
 
    # Iterate through all textareas
    for value in textareaboxes:
        # enter value
        value.send_keys(data[count])
        # increment count value
        count += 1
 
    # click on submit button
    submit = driver.find_element_by_xpath(
        '//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div/div/span/span')
    submit.click()
 
    # fill another response
    another_response = driver.find_element_by_xpath(
        '/html/body/div[1]/div[2]/div[1]/div/div[4]/a')
    another_response.click()
 
# close the window
driver.close()

Output:


Article Tags :