I've been trying to test out this google form filler but keep running into an issue with the submit button. This has never happened before and I am not sure why, every time I run the code it fills in all the boxes, however, it sometimes doesn't fill out the gender question and rarely or never hits submit or submit another response. The google form link is in the code below.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
import random
from time import sleep
from random import randint
import random
s=Service('/Users) #have the current service installed here
driver = webdriver.Chrome(service=s)
url="https://forms.gle/sNCNuebKtpdF6j7t6"
driver.get(url)
datas = [
]
for i in range(10):
sampleList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
val1 = random.choices(sampleList, weights=(5.8, 6.72, 7.5, 7.24, 6.8, 3.16, 3.5, 4.5, 1.2, 2.3), k=10) # 25,25,50
val2 = random.choices(sampleList, weights=(5.8, 6.7, 7.5, 7.2, 6.83, 9.1, 9.5, 8.5, 8.2, 8.3), k=10) # 25,25,50
val3 = random.choices(sampleList, weights=(5.8, 6.7, 7.52, 7.2, 6.8, 3.1, 4.5, 3.53, 1.2, 0.3), k=10) # 25,25,50
val4 = random.choices(sampleList, weights=(0.8, 2.7, 3.53, 3.2, 5.8, 4.12, 9.5, 9.5, 9.2, 9.3), k=10) # 25,25,50
val5 = random.choices(sampleList, weights=(1.78, 3.7, 5.5, 3.2, 6.8, 5.1, 9.51, 8.5, 8.2, 9.3), k=10) # 25,25,50
valx = random.randint(0,9)
datas.append([val1[valx],val2[valx],val3[valx],val4[valx],val5[valx]]) #this list prints out alright and gets the values needed
radiobuttons = driver.find_elements(By.CLASS_NAME, "appsMaterialWizToggleRadiogroupElContainer")
print(datas)
for data in datas: #for each mini list in the big list
print(data)
radiolist = [0, 1, 2, 3]
val0 = random.choices(radiolist, weights=(9.8,11.5,1.2,1.8), k=10)
print(val0)
rand = random.randint(0,9)
print(rand)
#radiobuttons[val0[rand]].click()
radiobuttons[1].click()
print(radiobuttons[val0[rand]])
count = 0 #count is 0
# contain input boxes
textboxes = driver.find_elements(By.CLASS_NAME, 'quantumWizTextinputPaperinputInput') #these textboxes and elements seem to work and get filled out
for value in textboxes:
# enter value
value.send_keys(data[count])
print(value)
# increment count value
count = 1
submit = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span[class*='appsMaterialWizButtonPaperbuttonContent ']"))).click() #this sometimes works but if so only once
# fill another response
print('hi')
another_response = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Submit another response'))).click()
#this does not work at all
#textboxes = browser.find_elements_by_class_name("quantumWizTextinputPaperinputInput")
#radiobuttons = browser.find_elements_by_class_name("docssharedWizToggleLabeledLabelWrapper")
#checkboxes = browser.find_elements_by_class_name("quantumWizTogglePapercheckboxInnerBox")
#submitbutton = browser.find_element_by_class_name("appsMaterialWizButtonPaperbuttonContent")
# close the window
driver.close()
CodePudding user response:
url="https://forms.gle/sNCNuebKtpdF6j7t6"
driver.get(url)
datas = []
for i in range(10):
sampleList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
val1 = random.choices(sampleList, weights=(5.8, 6.72, 7.5, 7.24, 6.8, 3.16, 3.5, 4.5, 1.2, 2.3), k=10) # 25,25,50
val2 = random.choices(sampleList, weights=(5.8, 6.7, 7.5, 7.2, 6.83, 9.1, 9.5, 8.5, 8.2, 8.3), k=10) # 25,25,50
val3 = random.choices(sampleList, weights=(5.8, 6.7, 7.52, 7.2, 6.8, 3.1, 4.5, 3.53, 1.2, 0.3), k=10) # 25,25,50
val4 = random.choices(sampleList, weights=(0.8, 2.7, 3.53, 3.2, 5.8, 4.12, 9.5, 9.5, 9.2, 9.3), k=10) # 25,25,50
val5 = random.choices(sampleList, weights=(1.78, 3.7, 5.5, 3.2, 6.8, 5.1, 9.51, 8.5, 8.2, 9.3), k=10) # 25,25,50
valx = random.randint(0,9)
datas.append([val1[valx],val2[valx],val3[valx],val4[valx],val5[valx]]) #this list prints out alright and gets the values needed
print(datas)
for data in datas: #for each mini list in the big list
radiobuttons = driver.find_elements(By.CLASS_NAME, "appsMaterialWizToggleRadiogroupElContainer")
print(data)
radiolist = [0, 1, 2, 3]
val0 = random.choices(radiolist, weights=(9.8,11.5,1.2,1.8), k=10)
print(val0)
rand = random.randint(0,9)
print(rand)
#radiobuttons[val0[rand]].click()
radiobuttons[1].click()
print(radiobuttons[val0[rand]])
count = 0 #count is 0
# contain input boxes
textboxes = driver.find_elements(By.CLASS_NAME, 'quantumWizTextinputPaperinputInput') #these textboxes and elements seem to work and get filled out
for value in textboxes:
# enter value
value.send_keys(data[count])
print(value)
# increment count value
count = 1
try:
submit = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Submit']")))
submit.click() #this sometimes works but if so only once
except Exception as e:
print(str(e))
# fill another response
print('hi')
try:
another_response = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, 'Submit another response')))
another_response.click()
except Exception as e:
print(str(e))
Your issues were in the radiobuttons as you need to regrab that element every loop due to page redirection so you ended up with stale elements and using the wrong selector for the submit button. With the following fixes it runs.

