-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUDIdriver.py
executable file
·88 lines (76 loc) · 3.68 KB
/
UDIdriver.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
#! /usr/bin/env python
#
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common import exceptions
import datetime, json, time, re, sys
class UdiDriver(object):
def __init__(self, conf):
self.conf = conf
options = webdriver.ChromeOptions()
self.driver = webdriver.Chrome(chrome_options=options)
self.driver.implicitly_wait(0)
def run(self):
driver = self.driver
driver.get("https://selfservice.udi.no/?epslanguage=en-GB")
driver.find_element_by_id(
"ctl00_BodyRegion_PageRegion_MainRegion_ctl00_heading").click()
username_elt = driver.find_element_by_id(
"ctl00_BodyRegion_LoginResponiveBox_txtUsername")
username_elt.clear()
username_elt.send_keys(self.conf["username"])
password_elt = driver.find_element_by_id("ctl00_BodyRegion_LoginResponiveBox_txtPassword")
password_elt.clear()
password_elt.send_keys(self.conf["password"])
driver.find_element_by_id(
"ctl00_BodyRegion_LoginResponiveBox_btnLocalLogin").click()
try:
driver.find_element_by_id("ctl00_BodyRegion_PageRegion_MainRegion_IconNavigationTile2_heading").click()
except exceptions.NoSuchElementException:
print('Incorrect username and/or password')
input('Press enter to continue...')
return False
driver.find_element_by_id("ctl00_BodyRegion_PageRegion_MainRegion_ApplicationOverview_applicationOverviewListView_ctrl0_btnBookAppointment").click()
driver.find_element_by_id("ctl00_PageRegion_MainContentRegion_ViewControl_spnReceiptAndBooking_BookingSummaryInfo_btnChangeBooking").click()
# We should be on the calendar page. Check this.
try:
driver.find_element_by_id("ctl00_BodyRegion_PageRegion_MainRegion_appointmentReservation_bookingHeader_lblTitle")
# print("We're at the calendar page!")
except exceptions.NoSuchElementException:
print('Not on the calendar page! Bombing out after a minute!')
time.sleep(60)
return False
# Keep looking for an open appointment till we find one.
while True:
try:
first_apt = driver.find_element_by_xpath("//td[@class='bookingCalendarBookedDay' or @class='bookingCalendarHalfBookedDay']/span[@class='dayNumber']")
day = int(first_apt.text)
month_year = driver.find_element_by_xpath('//*[@id="ctl00_BodyRegion_PageRegion_MainRegion_appointmentReservation_appointmentCalendar_pnlCalendarTop"]/div/div[3]/h2').text
found_date = datetime.datetime.strptime("%d %s" % (day, month_year), "%d %B %Y")
print found_date.strftime("%Y-%m-%d")
return found_date < self.conf["wait_if_earlier_than"]
except exceptions.NoSuchElementException:
print 'No appointment found: moving on'
driver.find_element_by_id('ctl00_BodyRegion_PageRegion_MainRegion_appointmentReservation_appointmentCalendar_btnNext').click()
def __del__(self):
self.driver.quit()
def main(driver=None, cfg_file=None):
if not driver:
driver = UdiDriver
if not cfg_file:
cfg_file = open(sys.argv[1])
with cfg_file as fd:
config = json.load(fd)
latest = datetime.datetime.strptime(
config["wait_if_earlier_than"], "%Y-%m-%d")
config["wait_if_earlier_than"] = latest
u = driver(config)
found_better_appt = u.run()
if found_better_appt:
# Wait for the user to book it
time.sleep(config.get('sleep_on_success', 3600))
return 0
else:
return 255
if __name__ == "__main__":
sys.exit(main())