该脚本只解决了单选,多选以及量表和矩阵量表,别的题目都没有改到,其次,如果多选有其他这个选项请修改代码,或者有最多和最少选择项规定,请修改源码。文章来源地址https://www.toymoban.com/news/detail-507009.html
from selenium import webdriver
import random
import time
import numpy as np
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
def get_ele_time(driver,time,func):
WebDriverWait(driver,time).until(func)
def auto_write():
# 防止被浏览器识别为脚本
# browser = webdriver.Chrome()
# browser.get("https://www.wjx.cn/vm/YsK8J1l.aspx")
chrome_options = Options()
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
# chrome_options.add_argument("--headless")
# chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument(
'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36')
driver = Chrome(options=chrome_options)
driver.get('https://www.wjx.cn/vm/PXD8Zlr.aspx')
# 获取到所有题目的选项
rank = driver.find_elements_by_class_name("field.ui-field-contain")
for i in range(len(rank)):
type_q = rank[i].get_attribute("type")
# 单选
if(type_q == '3'):
check = rank[i].find_elements_by_class_name("ui-radio")
mins = random.randrange(0,len(check)-1)
check[mins].click()
# 多选
if(type_q == '4'):
ui_check = rank[i].find_elements_by_class_name("label")
length = len(ui_check)
for k in range(len(ui_check)):
# 如果多选中填空题选项不叫其他,请更改此行代码
if ui_check[k].text == '其他':
length = length-1
# 如果多选规定选择的项数请修改下面的
random_choice = np.random.randint(1,length-1,size=random.randint(1,length))
for z in list(set(random_choice)):
ui_check[z].click()
# 量表
if(type_q == '5'):
lb_data = rank[i].find_elements_by_class_name("td")
lb_data[random.randint(0,len(lb_data)-1)].click()
# 矩阵量表
if(type_q == '6'):
# print(rank[i].text)
mat_data = rank[i].find_elements_by_css_selector("tr[tp=d]")
# mat_data = rank[i].find_elements_by_xpath("./tbody/tr[@tp=\"d\"]")
for s in range(0,len(mat_data)):
single_mat_row = mat_data[s].find_elements_by_class_name('rate-off.rate-offlarge')
# print(len(single_mat_row))
single_mat_row[random.randint(0,len(single_mat_row)-1)].click()
driver.find_element_by_id("ctlNext").click()
# 消除确认弹窗
time.sleep(random.randint(2, 5))
js = 'document.getElementById("alert_box").style.display="none";'
driver.execute_script(js)
# 点智能验证提示框,进行智能验证
driver.find_element_by_xpath("//div[@id='rectMask']").click()
#
#
time.sleep(random.randint(2, 5))
driver.quit() # 运行完毕自动关闭浏览器
# driver.quit()
if __name__ == '__main__':
for i in range(0,200):
print("正在填写第{}份问卷".format(i+1))
auto_write()
print("第{}份问卷已经填写完成".format(i+1))
time.sleep(random.randint(1,4))
文章来源:https://www.toymoban.com/news/detail-507009.html
到了这里,关于(问卷星)自动填写问卷星脚本python、selenium的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!