参考地址:https://blog.csdn.net/qq_44864262/article/details/106838604?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_baidulandingword-0&spm=1001.2101.3001.4242
1、获得调查问卷的网址,我这里用的是腾讯问卷
2、下载谷歌浏览器
3、下载对应版本的chromedriver
ChromeDriver下载地址:http://chromedriver.storage.googleapis.com/index.html
浏览器打开问卷网页,按F12打开控制台,点击右侧窗口左上角的图标,点击左侧元素查看。
点击右侧的标签,右键复制元素的XPath,存下来备用,每个题的不同选项只有很小的差别,因此每个题随便选一个选项复制其XPath即可(依据个人情况)。
思路:依据一定的概率生成问卷答案,用循环语句控制chormedriver对浏览器的各种操作,进而达到自动填写问卷的目的。这也可以用来做web测试和其他可以基于浏览器的自动化操作。
代码分析:
# 按给定的概率返回一个列表中的值
def number_of_certain_probability(sequence, probability):
x = random.uniform(0, 1)
cumulative_probability = 0.0
for item, item_probability in zip(sequence, probability):
# 只有当累加的概率比刚才随机生成的随机数大时候,才跳出,并输出此时对应的值
cumulative_probability += item_probability
if x < cumulative_probability:
break
return item
# 生成单选题的答案
def s_answear():
# 选项的值
value_list_1 = [1, 2, 3, 4, 5]
#1、2...单选题各选项的概率
probability_1 = [0.204, 0.314, 0.428, 0.054]
probability_2 = [0.171, 0.599, 0.091, 0.139]
# probability_n = [...]
probability = [
probability_1,
probability_2
# probability_n
]
s_answear_list = [] # 存放生成的选项
for i in range(5):
result = number_of_certain_probability(value_list_1[:len(probability[i])], probability[i])
s_answear_list.append(result)
return s_answear_list
# 生成多选题的答案
def d_answear():
# 值
value_list_2 = [1, 2, 3, 4, 5, 6, 7, 8]
# 多选题各选项单次被选择的概率(三选和双选)
answear_num = [3, 2] # 顺序存放每题最多答案数
null_answear = [0.15, 0.2] # 顺序存放每题答案少选的概率
probability_1 = [0.714 / 3, 0.381 / 3, 0.429 / 3, 0.452 / 3, 0.381 / 3, 0.214 / 3, 0.214 / 3, 0.214 / 3]
probability_2 = [0.714 / 3, 0.643 / 3, 0.569 / 3, 0.188 / 3, 0.476 / 3, 0.357 / 3, 0.124 / 3]
probability_3 = [0.633 / 2, 0.442 / 2, 0.26 / 2, 0.49 / 2, 0.071 / 2, 0.033 / 2]
probability = [
probability_1,
probability_2,
probability_3
]
d_answear_list = []
for i in range(5):
answear = []
for j in range(answear_num[i]):
result = number_of_certain_probability(value_list_2[:len(probability[i])], probability[i])
while (result in answear): # 使多选的结果不会重复
result = number_of_certain_probability(value_list_2[:len(probability[i])], probability[i])
answear.append(result)
d_answear_list.append(answear)
# 三选项的概率只选两项,使第三项为0,当选项为0是跳过选择以到达少选的目的
x = random.uniform(0, 1)
if x < null_answear[0]:
d_answear_list[0][2] = 0
x = random.uniform(0, 1)
if x < null_answear[1]:
d_answear_list[1][2] = 0
return d_answear_list
def write(num):
for i in range(num):
s_answear_list = s_answear()
d_answear_list = d_answear()
print(s_answear_list)
print(d_answear_list)
driver = webdriver.Chrome()
driver.get('问卷的链接')
# 第一个单选题
driver.find_element_by_xpath(
'//*[@id="question_q-1-B1GQ"]/div[2]/div[' + str(s_answear_list[0]) + ']/label/p').click()
# 第二个单选题
driver.find_element_by_xpath(
'//*[@id="question_q-2-fphY"]/div[2]/div[' + str(s_answear_list[1]) + ']/label/p').click()
# 第一个三选题
driver.find_element_by_xpath(
'//*[@id="question_q-4-B7dm"]/div[2]/div[' + str(d_answear_list[0][0]) + ']/label/p').click()
driver.find_element_by_xpath(
'//*[@id="question_q-4-B7dm"]/div[2]/div[' + str(d_answear_list[0][1]) + ']/label/p').click()
if d_answear_list[0][2] != 0:
driver.find_element_by_xpath(
'//*[@id="question_q-4-B7dm"]/div[2]/div[' + str(d_answear_list[0][2]) + ']/label/p').click()
js = "var q=document.documentElement.scrollTop=800" # 下拉像素(800是基于最顶端测算的距离)
driver.execute_script(js) # 执行下拉像素操作
# 第二个三选题
driver.find_element_by_xpath(
'//*[@id="question_q-5-HC0e"]/div[2]/div[' + str(d_answear_list[1][0]) + ']/label/p').click()
driver.find_element_by_xpath(
'//*[@id="question_q-5-HC0e"]/div[2]/div[' + str(d_answear_list[1][1]) + ']/label/p').click()
if d_answear_list[1][2] != 0:
driver.find_element_by_xpath(
'//*[@id="question_q-5-HC0e"]/div[2]/div[' + str(d_answear_list[1][2]) + ']/label/p').click()
# 第一个双选题
driver.find_element_by_xpath(
'//*[@id="question_q-7-G2IG"]/div[2]/div[' + str(d_answear_list[3][0]) + ']/label/p').click()
driver.find_element_by_xpath(
'//*[@id="question_q-7-G2IG"]/div[2]/div[' + str(d_answear_list[3][1]) + ']/label/p').click()
js = "var q=document.documentElement.scrollTop=3600"
driver.execute_script(js) # 下拉操作
# 提交按钮
driver.find_element_by_xpath(
'//*[@id="root-container"]/div/div[1]/div[2]/div[2]/div[5]/div/button/span').click()
print('第' + str(i) + '次填写成功')
driver.quit()
执行write(n)调用函数,n为需要自动填写的次数。