Selenium自动填写问卷星

Selenium自动填写问卷星

  • 代码
  • 注意

代码

Selenium自动填写问卷星_第1张图片

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
from pykeyboard import *
from pymouse import *

k = PyKeyboard()  # 键盘对象
m = PyMouse()     # 鼠标对象

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.maximize_window()
    driver.get('https://www.wjx.cn/vm/hDzGYdn.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))
            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(0, length, 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("layui-layer1").style.display="none";'
    driver.execute_script(js)
    '''
    time.sleep(random.randint(2, 5))
    m.click(1092, 617)  # 控制鼠标到屏幕的位置,然后点击


    # 点智能验证提示框,进行智能验证
    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))




注意

如果想要使谋个问题以特定的比例填写,只需要返回一个以指定概率返回的数的列表,替换下图中的位置。
Selenium自动填写问卷星_第2张图片
Selenium自动填写问卷星_第3张图片
以指定概率随机生成数的代码

import random
x = [i for i in range(10)]
p = [0.05]*4 + [0.1]*4 + [0.2,0.2]
def rate_random(data_list, rates_list):
    start = 0
    random_num = random.random()
    for idx, score in enumerate(rates_list):
        start += score
        if random_num <= start:
            break
    return data_list[idx]

print(rate_random(x,p))

Selenium自动填写问卷星_第4张图片

你可能感兴趣的:(Selenium,selenium)