-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtermin-bot.py
More file actions
380 lines (326 loc) · 15.8 KB
/
termin-bot.py
File metadata and controls
380 lines (326 loc) · 15.8 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#Quelle: https://selenium-python.readthedocs.io/
#!/usr/bin/env python
import config as c
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.firefox.service import Service
from fake_useragent import UserAgent
from datetime import datetime
from pathlib import Path
import undetected_chromedriver as uc
import random
import time
import os
import logging
logging.basicConfig(filename=c.install["LOGFILE"], level=logging.INFO)
OKGREEN = '\033[92m'
OKBLUE = '\033[94m'
ENDC = '\033[0m'
BOLD = "\033[1m"
HEADER = '\033[95m'
WARNING = '\033[93m'
FAIL = '\033[91m'
class Output:
def warn(self, message):
print('\t' + WARNING + message + ENDC+'\n')
def error(self, message):
print('\t' + FAIL + message + ENDC+'\n')
def info(self, message):
print('\t' + OKBLUE + message + ENDC+'\n')
def success(self, message):
print('\t' + OKGREEN + message + ENDC+'\n')
def passed(self, message):
print('\t' + BOLD + message + ENDC+'\n')
def header(self, message):
print('\t' + HEADER + message + ENDC+'\n')
class Search:
def __init__(self):
self.url = "http://www.python.org"
ua = UserAgent()
userAgent = ua.random
print(userAgent)
options = uc.ChromeOptions()
#options.add_argument('--headless=new')
options.add_argument(f'user-agent={userAgent}')
self.driver = uc.Chrome(options=options)
def test_search_in_python_org(self):
driver = self.driver
driver.get("http://www.python.org")
assert 'Python' in driver.title
elem = driver.find_element(By.NAME, 'q')
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
#el = WebDriverWait(driver, timeout=3).until(lambda d: d.find_element(By.TAG_NAME,"p"))
#assert el.text == "Hello from JavaScript!"
assert "No results found." not in driver.page_source
def getTerminByVisametric(self):
out = Output()
driver = self.driver
driver.get(c.legalization['landing_page'])
time.sleep(5) # Delay for 10 seconds.
try:
assert 'Visametric - Visa Application Center' in driver.title
except AssertionError:
if 'Just a moment...' in driver.title or 'Nur einen Moment...' in driver.title:
print('just a moment')
current_url = driver.current_url
while current_url == driver.current_url:
if c.install["isLinux"]:
os.system('spd-say "Waiting for human interaction."')
out.warn("Please resolve the captcha or press 'str+c' ")
out.warn('Waiting for human interaction.')
time.sleep(10) # Delay for 10 seconds.
if c.install["isLinux"]:
os.system('spd-say "Error. Page not found!"')
out.error("Error. Page not found!")
logging.error(str(datetime.now())+" Page not found: "+c.legalization['landing_page'])
return None
time.sleep(5) # Delay for 5 seconds.
#try:
# elem = driver.find_element(By.ID, 'recaptchaDisplay')
#except NoSuchElementException as e:
current_url = driver.current_url
#while 'Visametric - Visa Application Center' in driver.title :
while current_url == driver.current_url:
if c.install["isLinux"]:
os.system('spd-say "Waiting for human interaction."')
out.warn("Please resolve the captcha or press 'str+c' ")
out.warn('Waiting for human interaction.')
time.sleep(10) # Delay for 10 seconds.
out.info('captcha done')
#Push Button name="legalizationBtn"
#elem = driver.find_element(By.NAME, 'legalizationBtn')
#elem.click()
#Check OK Radio-Input name="surveyStart" id="result0"
WebDriverWait(driver, random.randint(10,25)).until(
EC.element_to_be_clickable((By.ID, 'result0'))
)
elem = driver.find_element(By.ID, 'result0')
elem.click()
#Check Iran Radio-Input name="nationality" id="result1"
WebDriverWait(driver, random.randint(10,25)).until(
EC.element_to_be_clickable((By.ID, 'result1'))
)
elem = driver.find_element(By.ID, 'result1')
elem.click()
# Captcha Part id="recaptcha-anchor"
#WebDriverWait(driver, random.randint(10,25)).until(
# EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@name, 'a-') and starts-with(@src, 'https://www.google.com/recaptcha')]"))
# )
#element = WebDriverWait(driver, random.randint(10,25)).until(
# EC.element_to_be_clickable((By.CSS_SELECTOR, "div.recaptcha-checkbox-checkmark"))
# )
#driver.execute_script("arguments[0].click();", element)
#current_url = driver.current_url
#elem = driver.find_element(By.ID, 'recaptcha-anchor')
#while elem.get_attribute("aria-checked") == 'false' :
# if c.install["isLinux"]:
# os.system('spd-say "Waiting for human interaction."')
# out.warn("Please resolve the captcha or press 'str+c' ")
# out.warn('Waiting for human interaction.')
# time.sleep(10) # Delay for 10 seconds.
#out.info('captcha done')
#driver.switch_to.default_content()
#To ignore google recaptcha
# driver.execute_script("$('#formAccessApplication').submit();")
WebDriverWait(driver, random.randint(10,25)).until(
EC.element_to_be_clickable((By.ID, 'btnSubmit'))
)
driver.find_element(By.ID, 'btnSubmit').click()
while current_url == driver.current_url:
try:
WebDriverWait(driver, random.randint(10,25)).until(EC.url_changes(current_url))
except:
out.warn("Please push the blue button. ")
time.sleep(2) # Delay for 2 seconds.
#Select input id="city"
select = Select(WebDriverWait(driver, random.randint(10,25)).until(
EC.presence_of_element_located((By.ID, 'city'))
))
#select = Select(driver.find_element(By.ID, 'city'))
select.select_by_visible_text(c.legalization["first_form"]['city'])
#Select input id="office"
select = Select(driver.find_element(By.ID, 'office'))
select.select_by_visible_text(c.legalization["first_form"]['office'])
#Select input id="officetype"
select = Select(driver.find_element(By.ID, 'officetype'))
select.select_by_visible_text(c.legalization["first_form"]['officetype'])
#Select input id="totalPerson"
select = Select(driver.find_element(By.ID, 'totalPerson'))
select.select_by_visible_text(c.legalization["first_form"]['totalPerson'])
#Check ATM Radio-Input id="atm"
WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.ID, 'atm'))
)
elem = driver.find_element(By.ID, 'atm')
elem.click()
#Input Cardnumber id="paymentCardInput"
WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.ID, 'paymentCardInput'))
)
elem = driver.find_element(By.ID, 'paymentCardInput')
elem.send_keys(c.legalization["first_form"]['paymentCardInput'])
#id="popupDatepicker2"
elemDatePicker = driver.find_element(By.ID, 'popupDatepicker2')
driver.execute_script("arguments[0].removeAttribute('readonly')", WebDriverWait(driver, 20).until(EC.element_to_be_clickable(elemDatePicker)))
elemDatePicker.clear()
elemDatePicker.send_keys(c.legalization["first_form"]['date'])
elem.click()
#Button id="checkCardListBtn"
driver.find_element(By.ID, 'checkCardListBtn').click()
time.sleep(2) # Delay for 2 seconds.
#Warnung
#"sa-icon sa-warning pulseWarning"
try:
#Check Radio-Input name="bankpayment"
WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.NAME, 'bankpayment'))
).click()
except TimeoutException:
eMessage = "Please check the config file. No payments were found."
if c.install["isLinux"]:
os.system('spd-say "'+eMessage+'"')
out.error(eMessage)
logging.error(str(datetime.now())+": "+eMessage)
return None
# try:
# assert 'Visametric - Visa Application Center' in driver.title
# except AssertionError:
current_url = driver.current_url
#button id="btnAppCountNext"
driver.find_element(By.ID, 'btnAppCountNext').click()
# while current_url == driver.current_url:
# try:
# WebDriverWait(driver, random.randint(10,25)).until(EC.url_changes(current_url))
# except:
# out.warn("Please push the blue button. ")
time.sleep(2) # Delay for 2 seconds.
#input id="scheba_number"
elem = WebDriverWait(driver, random.randint(10,25)).until(
EC.presence_of_element_located((By.ID, 'scheba_number'))
)
elem.send_keys(c.legalization["second_form"]['scheba_number'])
#input id="scheba_name" in persian letters!!!!
driver.find_element(By.ID, 'scheba_name').send_keys(c.legalization["second_form"]['scheba_name'])
#input id="name1"
driver.find_element(By.ID, 'name1').send_keys(c.legalization["second_form"]['name1'])
#input id="surname1"
driver.find_element(By.ID, 'surname1').send_keys(c.legalization["second_form"]['surname1'])
#Select input id="birthday1"
select = Select(driver.find_element(By.ID, 'birthday1'))
select.select_by_visible_text(c.legalization["second_form"]['birthday1'])
#Select input id="birthmonth1"
select = Select(driver.find_element(By.ID, 'birthmonth1'))
select.select_by_visible_text(c.legalization["second_form"]['birthmonth1'])
#Select input id="birthyear1"
select = Select(driver.find_element(By.ID, 'birthyear1'))
select.select_by_visible_text(c.legalization["second_form"]['birthyear1'])
#input id="passport1"
driver.find_element(By.ID, 'passport1').send_keys(c.legalization["second_form"]['passport1'])
#input id="phone1"
driver.find_element(By.ID, 'phone1').send_keys(c.legalization["second_form"]['phone1'])
#input id="phone21"
driver.find_element(By.ID, 'phone21').send_keys(c.legalization["second_form"]['phone21'])
#input id="email1"
driver.find_element(By.ID, 'email1').send_keys(c.legalization["second_form"]['email1'])
#link id="btnAppPersonalNext"
current_url = driver.current_url
#button id="btnAppPersonalNext"
driver.find_element(By.ID, 'btnAppPersonalNext').click()
#checkbox id="previewchk"
elem = WebDriverWait(driver, random.randint(10,25)).until(
EC.presence_of_element_located((By.ID, 'previewchk'))
)
elem.click()
#button id="btnAppPreviewNext"
driver.find_element(By.ID, 'btnAppPreviewNext').click()
time.sleep(2) # Delay for 2 seconds.
elem = WebDriverWait(driver, random.randint(10,25)).until(
EC.presence_of_element_located((By.ID, 'datepicker'))
)
elem.click()
#driver.switch_to.frame(driver.find_elements(By.CSS_SELECTOR,"div .datepicker-days"))
foundTermin = False
countMonth = 0
while countMonth < 3:
dayElems = driver.find_elements(By.CSS_SELECTOR,".day")
monthElem = driver.find_element(By.CSS_SELECTOR,'.datepicker-switch').get_attribute('innerHTML')
for dayElem in dayElems:
value = dayElem.get_attribute('innerHTML')
if "disabled" in dayElem.get_attribute('class'):
#print (value +" "+monthElem+" Element is not clickable")
i = 0
else:
dayElem.click()
print (value +" "+monthElem+" Element is clickable")
foundTermin = True
break
#For end
if foundTermin:
break
#Next Month Button
#<th class="next" style="visibility: visible;">»</th>
if countMonth < 2:
driver.find_element(By.CSS_SELECTOR, '.next').click()
countMonth += 1
#While End
if foundTermin:
#button id= btnAppCalendarNext
driver.find_element(By.ID, 'btnAppCalendarNext').click()
message = "Es wurde ein Termin gefunden!!!!"
if c.install["isLinux"]:
os.system('spd-say "'+message+'"')
out.success(str(datetime.now())+" "+message)
logging.info(str(datetime.now())+" "+message)
else:
out.info(str(datetime.now())+" Leider keine Termine gefunden.")
logging.info(str(datetime.now())+" Leider keine Termine gefunden.")
return foundTermin
#to refresh the browser
#driver.refresh()
# Zeit Element -> id="watch"
#<span id="watch"><b>7m 6s </b></span>
#Abgelaufener Tag
#<td class="old disabled day disabled">28</td>
#TZoday
#<td class="today disabled day disabled">14</td>
#disabled Day
# <td class="day disabled">19</td>
#tag nächster Monat
# <td class="new day disabled">1</td>
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
termin = False
while termin == False:
s = Search()
termin = s.getTerminByVisametric()
if termin == None:
termin = False
time.sleep(5) # Delay for 5 seconds.
if not termin:
s.tearDown()
del(s)
else:
out = Output()
message = "closing window at 20 minutes."
out.success(str(datetime.now())+" "+message)
logging.info(str(datetime.now())+" "+message)
time.sleep(600) # Delay for 600 seconds.
message = "The browser window is closing at 10 minutes."
if c.install["isLinux"]:
os.system('spd-say "'+message+'"')
out.success(str(datetime.now())+" "+message)
logging.info(str(datetime.now())+" "+message)
time.sleep(600) # Delay for 600 seconds.
message = "closing window."
out.success(str(datetime.now())+" "+message)
logging.info(str(datetime.now())+" "+message)
# s.test_search_in_python_org()
# s.tearDown()