-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeitman_auto.py
159 lines (122 loc) · 4.48 KB
/
zeitman_auto.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
#!/usr/bin/env python3
"""
Automatically fill out zeitweb form
"""
import getpass
import random
import time
import os
from selenium.common.exceptions import (
UnexpectedTagNameException,
NoSuchElementException,
)
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from webdriver_manager.chrome import ChromeDriverManager
# import config.py file - copy template if doesn't exist
if not os.path.exists("config.py"):
print("config.py file does not exist: copying template!")
os.popen("cp config_template.py config.py")
time.sleep(1) # otherwise config is not imported correctly
import config
# user information
usr = input("Username: ")
pswd = getpass.getpass("Password: ")
# launch zeitweb
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://zeitweb.hzdr.de/scripts_zeitm/login.php?sso=2")
# login
usr_elem = driver.find_element("name", "username")
usr_elem.clear()
usr_elem.send_keys(usr)
pswd_elem = driver.find_element("name", "passwort")
pswd_elem.send_keys(pswd)
submit = driver.find_element("id", "button")
submit.click()
# go to chosen month
monthstr = "jahrmonat(" + str(config.year) + "," + str(config.month) + ', "E")'
timesheets = driver.find_elements("class name", "statustd")
# search until chosen month is found
for timesheet in timesheets:
onclick = timesheet.get_attribute("onclick")
if onclick == monthstr:
timesheet.click()
break
# max no of days in month
ndays = 31
# switch to the main table frame
frame = driver.find_element("name", "Hauptfenster")
driver.switch_to.frame(frame)
def random_time(hour, minute, noise):
# add in the random noise
randtime = 10 * random.randint(-noise, noise)
if minute + randtime >= 60:
hour = hour + 1
minute = (minute + randtime) % 60
elif minute + randtime <= 0:
hour = hour - 1
minute = (minute + randtime) % 60
else:
minute = minute + randtime
return hour, minute
# loop over days in the month
for n in range(1, ndays + 1):
# randomise start and end times
rand_starthr, rand_startmin = random_time(
config.starthr, config.startmin, config.rand_start
)
rand_endhr, rand_endmin = random_time(config.endhr, config.endmin, config.rand_end)
# exception raised if holiday or month < 31 days
try:
# check that day is a normal working day
type_id = "anab" + str(n)
type_elem = Select(driver.find_element("name", type_id))
default_text = type_elem.first_selected_option.text
# start hour
strthr_id = "tf_vonSS" + str(n)
strthr_elem = Select(driver.find_element("name", strthr_id))
# start minute
strtmin_id = "tf_vonMM" + str(n)
strtmin_elem = Select(driver.find_element("name", strtmin_id))
# end hour
endhr_id = "tf_bisSS" + str(n)
endhr_elem = Select(driver.find_element("name", endhr_id))
# end minute
endmin_id = "tf_bisMM" + str(n)
endmin_elem = Select(driver.find_element("name", endmin_id))
# input data if day is normal working day
autofill_types = ["not entered", "mobile working"]
overwrite_types = ["present", "mobile working"]
# if data should be filled
if default_text in autofill_types:
strthr_elem.select_by_value(str(rand_starthr))
strtmin_elem.select_by_value(str(rand_startmin))
endhr_elem.select_by_value(str(rand_endhr))
endmin_elem.select_by_value(str(rand_endmin))
# enter "present" which has key "9999"
if default_text == "not entered":
type_elem.select_by_value("9999")
# if overwriting data
elif default_text in overwrite_types and config.overwrite_data:
strthr_elem.select_by_value(str(rand_starthr))
strtmin_elem.select_by_value(str(rand_startmin))
endhr_elem.select_by_value(str(rand_endhr))
endmin_elem.select_by_value(str(rand_endmin))
except UnexpectedTagNameException:
pass
except NoSuchElementException:
break
# if save and exit
if config.save_and_exit:
# sleep for a second
time.sleep(1)
# switch back to main frame
driver.switch_to.parent_frame()
# switch to navigation frame
frame = driver.find_element("name", "Navigation")
driver.switch_to.frame(frame)
# click save button
save_button = driver.find_element("name", "Save_az")
save_button.click()
# close browser
driver.close()