This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathubank.py
executable file
·99 lines (80 loc) · 3.4 KB
/
ubank.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
#!/usr/bin/env python3
# coding: utf-8
import time
import json
import tempfile
import glob
import os
from selenium import webdriver
import selenium
downloads_dir = tempfile.mkdtemp()
def save_csv(browser):
while True:
try:
e = browser.find_element_by_class_name('excell')
break
except selenium.common.exceptions.NoSuchElementException:
time.sleep(0.5)
print('.')
e.click()
# sleep a bit to let firefox actually save the file - can use inotify? or
# maybe there is a way to acess the browser's download list directly and
# avoid races.
time.sleep(0.5)
files = glob.glob(os.path.join(downloads_dir, '*.csv'))
by_mtime = list(sorted([(os.stat(f).st_mtime, f) for f in files]))
return by_mtime[-1][1]
def get_credentials():
cred = json.load(open('credentials.json'))
username = cred['username']
password = cred['password']
return username, password
def setup_profile():
profile = webdriver.FirefoxProfile()
profile.default_preferences['browser.download.folderList'] = 2
profile.default_preferences['browser.download.dir'] = downloads_dir
profile.default_preferences['browser.helperApps.neverAsk.saveToDisk'] = 'application/vnd.ms-excel,text/csv,application/csv,application/x-csv,application/vnd.csv'
return profile
def wait_for_url_change(browser, prev_url):
print("waiting for url change from {}".format(prev_url))
while browser.current_url == prev_url:
time.sleep(1)
def login(browser, username, password):
login_iframe = browser.find_element_by_id('LoginIframeTag')
browser.switch_to_frame(login_iframe)
username_element = browser.find_element_by_class_name('fibi_username')
username_element.clear()
username_element.send_keys(username)
password_element = browser.find_element_by_class_name('fibi_password')
password_element.clear()
password_element.send_keys(password)
cur_url = browser.current_url
password_element.send_keys('\n')
wait_for_url_change(browser, cur_url)
# This fails: 1) need to wait for matafTools to be available, so wait for load
# of a certain javascript file, probably just wait for load of the button
# invoking this script 2) fedora/firefox crashes when debugging this script 3)
# setting of profile parameters above does not work, probably wrong mime type,
# also maybe wrong way to setup the preferences.
#browser.execute_script("matafTools.processSaveAs('811','csv','csv');")
def main():
print("using selenium {}".format(selenium.__version__))
profile = setup_profile()
browser = webdriver.Firefox(firefox_profile=profile)
browser.get('https://online.u-bank.net/wps/portal/FibiMenu/Home')
print("session id {}".format(browser.session_id))
username, password = get_credentials()
login(browser, username, password)
status_csv = save_csv(browser)
print("status: {}".format(status_csv))
# This part is slow due to many accesses via selenium? can fix the XPATH to
# make it faster. Full text is TNUOT BAHESHBON - תנועות בחשבון
spans = browser.find_elements_by_xpath('//span')
operations_span = [s for s in spans if 'בחשבון' in s.text][0]
current_url = browser.current_url
operations_span.click()
wait_for_url_change(browser, current_url)
operations_csv = save_csv(browser)
print("operations: {}".format(status_csv))
if __name__ == '__main__':
main()