欢迎来到ZeroAO的博客

竹杖芒鞋轻胜马,谁怕?一蓑烟雨任平生。
扩大
缩小

Python+Selenium自动刷问卷星问卷

朋友让帮忙答下问卷星,因嫌麻烦就干脆写个脚本帮他玩了

我们需要用到selenium库,接下来分析下问卷


 每个问题ID不同,我们可以通过id来找到每个小问,再来找到详细标签

访问该地址

driver.get('https://www.wenjuan.com/s/IFNFni
访问该标签,random.randint让其随机选择任意一选项
xpath3 = '//*[@id="question_5e647ab73631f215eee34179"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 5)

多选类似

 添加for随机循环几次执行

填空题

直接传值进去就行,把值存到一个列表,在随机存到key_2里

 driver.find_element_by_id("5e647ab73631f215eee341d0").send_keys(key_2)

提交

submit = driver.find_elements_by_id('next_button')[0]
submit.click()

间隔1秒然后退出

time.sleep(1)
driver.quit()

完整代码如下

 

import time
import random
from selenium import webdriver



def autoSelect():
    # 将问卷星网站放在下面
    driver.get('https://www.wenjuan.com/s/IFNFni')

    key_1 = str(random.randint(1, 4))
    list1 = ['计算机', '会计', '汽修', '幼师', '建筑', '工商']
    key_2 = list1[random.randint(0,5)]


    # 单选题,因为和后面的题有逻辑关系,记下随机值key_1
    xpath1 = '//*[@id="question_5e647ab73631f215eee34177"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % key_1
    answer_1 = driver.find_elements_by_xpath(xpath1)[0]
    answer_1.click()

    # 2
    for i in range(random.randint(2, 5)):
        xpath2 = '//*[@id="question_5e647ab73631f215eee34178"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 6)
        answer_2 = driver.find_elements_by_xpath(xpath2)[0]
        answer_2.click()

    # 3
    xpath3 = '//*[@id="question_5e647ab73631f215eee34179"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 5)
    answer_3 = driver.find_elements_by_xpath(xpath3)[0]
    answer_3.click()

    # 4
    xpath4 = '//*[@id="question_5e647ab73631f215eee3417a"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 5)
    answer_4 = driver.find_elements_by_xpath(xpath4)[0]
    answer_4.click()

    # 5
    xpath5 = '//*[@id="question_5e647ab73631f215eee3417b"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 5)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 6
    xpath5 = '//*[@id="question_5e647ab73631f215eee3417c"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 4)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 7
    xpath5 = '//*[@id="question_5e647ab73631f215eee3417d"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 6)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 8
    xpath5 = '//*[@id="question_5e647ab73631f215eee3417e"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 4)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 9
    xpath5 = '//*[@id="question_5e647ab73631f215eee3417f"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 4)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 10
    xpath5 = '//*[@id="question_5e647ab73631f215eee34180"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 4)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 11
    xpath5 = '//*[@id="question_5e647ab73631f215eee34181"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 5)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 12
    xpath5 = '//*[@id="question_5e647ab73631f215eee34182"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 4)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 13
    for i in range(1,4):
        xpath5 = '//*[@id="question_5e647ab73631f215eee34183"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 5)
        answer_5 = driver.find_elements_by_xpath(xpath5)[0]
        answer_5.click()

    # 14
    xpath5 = '//*[@id="question_5e647ab73631f215eee34184"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 3)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 15
    for i in range(2, 4):
        xpath5 = '//*[@id="question_5e647ab73631f215eee34185"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 4)
        answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 16

    driver.find_element_by_id("5e647ab73631f215eee341d0").send_keys(key_2)



    # 17
    xpath5 = '//*[@id="question_5e647ab73631f215eee34187"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 4)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 18
    xpath5 = '//*[@id="question_5e647ab73631f215eee34188"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 5)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 19
    xpath5 = '//*[@id="question_5e647ab73631f215eee34189"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 2)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()

    # 20
    for i in range(0, 5):
        xpath5 = '//*[@id="question_5e647ab73631f215eee3418a"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 6)
        answer_5 = driver.find_elements_by_xpath(xpath5)[0]
        answer_5.click()

    # 21
    xpath5 = '//*[@id="question_5e647ab73631f215eee3418b"]/div[2]/div[3]/div[1]/div[%s]/label[1]' % random.randint(1, 3)
    answer_5 = driver.find_elements_by_xpath(xpath5)[0]
    answer_5.click()



    # 提交
    submit = driver.find_elements_by_id('next_button')[0]
    submit.click()

    time.sleep(1)
    driver.quit()

if __name__ == '__main__':
    # 循环4次
    for index in range(1, 5):
        driver = webdriver.Chrome()
        autoSelect()

 

由于时间关系,就简单写了下,还要许多未完善的,能先用着

 

posted on 2020-03-26 16:08  Zeroao  阅读(3851)  评论(6编辑  收藏  举报

导航

Live2D