forked from vxwong/spain-visa-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisa.py
More file actions
95 lines (83 loc) · 3.84 KB
/
visa.py
File metadata and controls
95 lines (83 loc) · 3.84 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
from datetime import datetime
from utils.basic import Basic
from utils.log import logger
from utils import config
from utils.decorators import singleton
class Visa(Basic):
def __init__(self, driver):
super().__init__(driver)
def open_page(self, page):
self.driver.get(page)
def select_centre(self, county, city, category):
self.wait_for_secs()
self.click_el(name="JurisdictionId")
self.click_el(xpath="//select[@name='JurisdictionId']/option[contains(text(),'{}')]".format(county))
self.wait_for_loading()
self.click_el(name="centerId")
self.click_el(xpath="//select[@name='centerId']/option[contains(text(),'{}')]".format(city))
self.wait_for_secs()
self.click_el(name="category")
self.click_el(xpath="//select[@name='category']/option[contains(text(),'{}')]".format(category))
self.wait_for_secs()
self.click_el(name='checkDate')
logger.info("select centre finished")
def go_to_appointment_page(self, phone='', email=''):
self.open_page(config.OPENED_PAGE)
# self.select_centre("England", "Manchester", "Normal")
# self.enter_phone_and_email(phone, email)
# self.enter_wrong_code(email, config.PASSWORD)
# self.enter_code_from_email(email)
def login(self):
try:
# self.click_el(xpath="//a[text() = 'Log in']")
element = self.driver.find_element_by_xpath("//a[contains(text(),'Log in')]")
element.click()
self.wait_for_secs(5)
self.enter_message(config.EMAIL, name='email')
self.wait_for_secs()
self.enter_message(config.PASSWORD, name='password')
self.wait_for_secs()
self.click_el(name="login")
logger.info("log in finished")
except Exception as e:
logger.error(e)
def go_to_book_appointment(self):
unique_suffix = config.OPENED_PAGE.split('/')[-1]
link = f'book-appointment/{unique_suffix}'
logger.info(f"date appointment link = [{link}]")
# open a new tab
self.driver.execute_script(f'window.open(\"{link}\","_blank");')
# switch to the new tab
self.driver.switch_to.window(self.driver.window_handles[-1])
logger.info("go to book appointment finished")
def check_available_dates(self):
self.click_el(id="VisaTypeId")
self.click_el(xpath="//select[@id='VisaTypeId']/option[contains(text(),'{}')]".format(config.VISA_TYPE))
self.wait_for_secs()
# check date
self.click_el(id="app_date")
available_dates = {}
next_button_xpath = "//div[@class = 'datepicker-days']//th[@class = 'next' and @style = 'visibility: visible;']" # next month
while True:
nd = self.get_normal_dates()
if nd:
available_dates.update(nd)
if self.driver.find_elements_by_xpath(next_button_xpath):
self.wait_for_secs()
self.click_el(xpath=next_button_xpath)
else:
break
return available_dates
def get_normal_dates(self):
normal_dates_xpath = "//div[@class='datepicker-days']//td[not(contains(@class, 'disabled'))]" # days in the current month
result_dates = {}
dates = []
if len(self.driver.find_elements_by_xpath(normal_dates_xpath)):
found_month = self.driver.find_element_by_xpath(
"//div[@class='datepicker-days']//th[@class='datepicker-switch']").text
for day in self.driver.find_elements_by_xpath(normal_dates_xpath): # need refactor fix
dates.append(day.text)
for day in dates:
found_date = datetime.strptime(day + " " + found_month, '%d %B %Y')
result_dates[found_date.strftime("%d/%m/%Y")] = []
return result_dates