forked from AiRanthem/xmu-daily-report
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.py
193 lines (132 loc) · 6.21 KB
/
job.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import time
from typing import Callable
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import webdriver
from log import logger
from utils import fail
NULL = '请选择'
browser = webdriver.get()
def must_operate_element(driver: WebDriver, by: str, target: str, do: Callable, comment: str):
try:
target = WebDriverWait(driver, 10).until(
lambda x: x.find_element(by, target))
result = do(target)
logger.info(f"{comment} 成功")
return result
except Exception as e:
fail(f"{comment} 失败", "", "", e, False, True)
def xpath_exists(driver: WebDriver, xpath: str, comment: str) -> bool:
try:
must_operate_element(driver, By.XPATH, xpath, lambda x: x, f"确认 {comment} 存在")
return True
except:
return False
def click_given_xpath(driver: WebDriver, xpath: str, comment: str):
must_operate_element(driver, By.XPATH, xpath, lambda x: x.click(), "点击 " + comment)
def get_text(driver: WebDriver, xpath: str, comment: str) -> str:
return must_operate_element(driver, By.XPATH, xpath, lambda x: x.text, f"获取 {comment} 文本")
def set_text(driver: WebDriver, xpath: str, text: str, comment: str):
must_operate_element(driver, By.XPATH, xpath, lambda x: x.send_keys(text), f"填写 {comment} 文本")
def select_dropdown(driver: WebDriver, dropdown_xpath: str, target_xpath: str, comment: str):
click_given_xpath(driver, dropdown_xpath, f"{comment} 下拉框")
time.sleep(1)
click_given_xpath(driver, target_xpath, f"{comment} 选项")
class Job:
def __init__(self, driver: WebDriver, comment: str):
self.driver = driver
self.comment = comment
self.children = []
def should_do(self) -> bool:
...
def do(self):
if self.should_do():
logger.info(f"执行任务 {self.comment}")
self._do()
time.sleep(1)
for child in self.children:
child.do()
else:
logger.info(f"跳过任务 {self.comment}")
def _do(self):
...
def add_child(self, *jobs):
self.children.extend(jobs)
return self
class DropdownJob(Job):
def __init__(self, driver: WebDriver, dropdown_xpath: str, target_xpath: str, comment: str):
super().__init__(driver, comment)
self.dropdown_xpath = dropdown_xpath
self.target_xpath = target_xpath
def should_do(self) -> bool:
try:
return NULL in get_text(self.driver, self.dropdown_xpath, self.comment)
except Exception:
return False
def _do(self):
select_dropdown(self.driver, self.dropdown_xpath, self.target_xpath, self.comment)
time.sleep(1)
class TextJob(Job):
def __init__(self, driver: WebDriver, xpath: str, text: str, comment: str):
super().__init__(driver, comment)
self.xpath = xpath
self.text = text
def should_do(self) -> bool:
try:
return xpath_exists(self.driver, self.xpath, self.comment)
except Exception:
return False
def _do(self):
set_text(self.driver, self.xpath, self.text, self.comment)
class ClickJob(Job):
def __init__(self, driver: WebDriver, xpath: str, comment: str):
super(ClickJob, self).__init__(driver, comment)
self.xpath = xpath
def should_do(self) -> bool:
return xpath_exists(self.driver, self.xpath, self.comment)
def _do(self):
click_given_xpath(self.driver, self.xpath, self.comment)
def click_mytable() -> Job:
return ClickJob(browser, '//*[@id="mainM"]/div/div/div/div[1]/div[2]/div/div[3]/div[2]', "我的表单")
def dropdown_province(data: str) -> Job:
return DropdownJob(browser, '//*[@id="address_1582538163410"]/div/div[1]/div/div',
f'//label[@title="{data}"][1]', '省')
def dropdown_city(data: str) -> Job:
return DropdownJob(browser, '//*[@id="address_1582538163410"]/div/div[2]/div/div', f'//label[@title="{data}"][1]',
'市')
def dropdown_district(data: str) -> Job:
return DropdownJob(browser, '//*[@id="address_1582538163410"]/div/div[3]/div/div',
f'//label[@title="{data}"][1]',
'区')
def dropdown_confirm() -> Job:
return DropdownJob(browser, "//*[@id='select_1582538939790']/div/div/span[1]", "/html/body/div[8]/ul/div/div["
"3]/li/label", '本人承诺')
def dropdown_inschool(data: str) -> Job:
return DropdownJob(browser, '//*[@id="select_1611108284522"]/div/div', f'//label[@title="{data}"][1]', "是否在校")
def dropdown_campus(data: str) -> Job:
return DropdownJob(browser, '//*[@id="select_1582538643070"]/div/div/span[1]',
f'//label[starts-with(@title, "{data}")][1]',
"校区")
def dropdown_stay_in_school(data: str) -> Job:
return DropdownJob(browser, '//*[@id="select_1611110401193"]/div/div',
f'//label[starts-with(@title, "{data}")][1]',
"是否住在校内")
def dropdown_indorm(data: str) -> Job:
return DropdownJob(browser, '//*[@id="select_1611108377024"]/div/div',
f'//label[starts-with(@title, "{data}")][1]',
"是否住在校内学生宿舍")
def dropdown_building(data: str) -> Job:
return DropdownJob(browser, '//*[@id="select_1611108445364"]/div/div',
f'//label[starts-with(@title, "{data}")][1]',
"楼栋")
def text_room(data: str) -> Job:
return TextJob(browser, '//*[@id="input_1611108449736"]/input', data, "房间号")
def drowdown_live_in_xiamen(data: str) -> Job:
return DropdownJob(browser, '//*[@id="radio_1611108503484"]/div/div',
f'//label[starts-with(@title, "{data}")][1]',
"是否家在厦门")
def text_address(data: str) -> Job:
return TextJob(browser, '//*[@id="input_1611108489669"]/input', data, "校外住址")
def click_save() -> Job:
return ClickJob(browser, "//span[starts-with(text(),'保存')][1]", "保存")