-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
61 lines (42 loc) · 1.65 KB
/
main.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
from selenium import webdriver
import random
driver = webdriver.Chrome()
# driver = webdriver.Chrome(executable_path="C:/chromedriver.exe")
#安装目录不在python目录下解决办法
driver.get('https://www.baidu.com')
#打开一个主标签页
for i in range(1):
js = "window.open('https://www.wjx.cn/m/99634699.aspx')"
#定义一个js语句传给driver.execute_scrip执行:打开新标签页
driver.execute_script(js)
#driver.get('https://www.wjx.cn/m/99634699.aspx')
handlers =driver.window_handles
driver.switch_to_window(handlers[1])
# 获得所有问题的页数
questions = driver.find_elements_by_xpath('//div[@id="divQuestion"]/fieldset')
# 循环每一页
for question in questions:
# 获得此页所有问题
qs = question.find_elements_by_xpath('./div[@class="field ui-field-contain"]')
# 循环每个问题
for q in qs:
####先滑到标签再去点击
driver.execute_script("arguments[0].scrollIntoView();",q)
### 找到标签下的所有答案
ans=q.find_elements_by_xpath('./div[2]/div[@class="ui-radio"]')
# 随机选择一个答案
a = random.choice(ans)
# 点击
a.click()
try: # 判断是否是最后一页
# 点击下一页
next = driver.find_elements_by_xpath('//div[@id="divNext"]')[0]
next.click()
except:
pass
# 回答完所有问题后提交
sub = driver.find_elements_by_xpath('//div[@id="ctlNext"]')[0]
sub.click()
#driver.close()
#driver.switch_to_window(handlers[0])
#driver.quit()