如何修复 python google 表单和 selenium 提交错误?
How to fix python google form and selenium submit error?
我一直在尝试测试这个 google 表单填充器,但 运行 一直在使用提交按钮时遇到问题。这以前从未发生过,我不确定为什么,每次我 运行 它填充所有框的代码,但是,它有时不会填写性别问题并且很少或从不点击提交或提交另一个回复。 google 形式 link 在下面的代码中。
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()
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))
您的问题出在单选按钮中,因为由于页面重定向,您需要在每个循环中重新抓取该元素,因此您最终得到了陈旧的元素并为提交按钮使用了错误的选择器。通过以下修复,它可以运行。
我一直在尝试测试这个 google 表单填充器,但 运行 一直在使用提交按钮时遇到问题。这以前从未发生过,我不确定为什么,每次我 运行 它填充所有框的代码,但是,它有时不会填写性别问题并且很少或从不点击提交或提交另一个回复。 google 形式 link 在下面的代码中。
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()
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))
您的问题出在单选按钮中,因为由于页面重定向,您需要在每个循环中重新抓取该元素,因此您最终得到了陈旧的元素并为提交按钮使用了错误的选择器。通过以下修复,它可以运行。