forked from Zemelee/wjx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwjx1.py
102 lines (92 loc) · 3.76 KB
/
wjx1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# 问卷网址
url = 'https://www.wjx.cn/vm/h4eAvAV.aspx'
# 生成m到n之间的o个不重复的数字列表,用于多选题
# m指1,n指多选题的选项个数,o为小于n大于等于1的随机数
def int_random(m, n, o):
p = []
while len(p) < o:
new_int = random.randint(m, n)
if new_int not in p:
p.append(new_int)
return p
def run():
# 躲避智能检测
option = webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=option)
driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument',
{'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})'
})
driver.get(url)
# 总共有28个题目
i = 1
while i <= 27:
# 单选、多选xpath //*[@id="div21"]/div[2]/div[2]
# 通过xpath循环遍历每一道题
base_xpath1 = '//*[@id="div{}"]'.format(i)
base_xpath3 = base_xpath1 + '/div[2]/div'
# a表示所有选项的div标签
a = driver.find_elements(By.XPATH, base_xpath3)
# b表示在选项长度之内的随机数
b = random.randint(1, len(a))
# 1-21题均为单选 https://www.wjx.cn/vm/h4eAvAV.aspx
if i <= 21:
# 通过随机数字,点击该数字的选项
driver.find_element(By.CSS_SELECTOR,
'#div{} > div.ui-controlgroup > div:nth-child({})'.format(i, b)).click()
# 多选题 i-->22-27
elif i in range(22, 28):
# 生成b个1到选项长度len(a)个随机数
q = int_random(1, len(a), b)
# sort函数表示将列表排序,如果未加参数表示从小到大排列
q.sort()
for r in q:
# 模拟点击多选题中与r对应的选项
driver.find_element_by_css_selector(
'#div{} > div.ui-controlgroup > div:nth-child({})'.format(i, r)).click()
i += 1
# 28题排序题 //*[@id="div28"]/ul/li[1]
# !!!!!!排序题暂时不能完美刷过
base_xpath1 = '//*[@id="div{}"]'.format(i)
base_xpath3 = base_xpath1 + '/ul/li'
a = driver.find_elements(By.XPATH, base_xpath3)
q = int_random(1, len(a), len(a))
for r in q:
try:
driver.find_element_by_css_selector(
'#div28 > ul.ui-controlgroup > li:nth-child({})'.format(r)).click()
except:
pass
# 第29题填空题
driver.find_element_by_css_selector('#q29').send_keys('暂无')
# ------------------------------------------------------------------------
time.sleep(0.5)
# 点击提交
driver.find_element(By.XPATH, '//*[@id="ctlNext"]').click()
# 出现点击验证码验证
time.sleep(0.5)
try:
# 点击对话框的确认按钮
driver.find_element(By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]').click()
time.sleep(0.5)
# 点击智能检测按钮
driver.find_element(By.XPATH, '//*[@id="SM_BTN_1"]').click()
time.sleep(0.5)
except:
print("无智能验证")
# 关闭页面
time.sleep(4)
handles = driver.window_handles
driver.switch_to.window(handles[0])
# 关闭当前页面,如果只有一个页面,则也关闭浏览器
driver.close()
count = 0
while True:
run()
count += 1
print('已填写{}份-{}'.format(count, time.strftime('%H:%M:%S', time.localtime(time.time()))))