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

19 Aug, 2021
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
Address
Phone Number
Comments
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’],]
To achieve our required functionality given steps needs to followed in a perfect order:
Import selenium and time module
Add chrome driver path and Form URL
Add some delay, until page gets fully loaded
Add data in the form of list
Iterate through each data and fill detail
Close the window
Program:
Python3
# Import Modulefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport time # open Chromedriver = webdriver.Chrome(    'C:/Users/HP/Desktop/Drivers/chromedriver_win32/chromedriver.exe') # Open URLdriver.get('https://forms.gle/vWVmojtWdfFvEj8V6') # wait for one second, until page gets fully loadedtime.sleep(1) # Datadatas = [    ['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 datafor 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 windowdriver.close()
Output:
gulshankumarar231
Python Selenium-Exercises
Python-selenium
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Python | Get unique values from a list
Defaultdict in Python
Python | os.path.join() method
Python Classes and Objects
Create a directory in Python