(问卷星)自动填写问卷星脚本python、selenium

该脚本只解决了单选,多选以及量表和矩阵量表,别的题目都没有改到,其次,如果多选有其他这个选项请修改代码,或者有最多和最少选择项规定,请修改源码。文章来源地址https://uudwc.com/A/Y1zdx

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://blog.csdn.net/qq_45702175/article/details/125172632

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

h
上一篇 2023年06月27日 19:07
下一篇 2023年06月27日 19:10