-
Notifications
You must be signed in to change notification settings - Fork 0
/
O1-Selenium-Export-Script.py
1502 lines (1367 loc) · 56.5 KB
/
O1-Selenium-Export-Script.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import logging
import os
import pickle
import sys
import time
import warnings
from os import environ as env
from zipfile import ZipFile
import colorlog
from dotenv import load_dotenv
from selenium import webdriver
from selenium.common.exceptions import (
ElementClickInterceptedException,
ElementNotInteractableException,
ElementNotVisibleException,
NoSuchAttributeException,
NoSuchElementException,
StaleElementReferenceException,
TimeoutException,
WebDriverException,
)
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.remote_connection import LOGGER
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
warnings.filterwarnings("ignore")
LOGGER.setLevel(logging.WARNING)
# Load environment variables #
load_dotenv()
os.environ["WDM_LOG"] = "false"
logging.getLogger("WDM").setLevel(logging.NOTSET)
# GLOBALS CONSTANTS/VARIABLES #
COOKIES_FILE = env.get("COOKIES_FILE")
PROJ_URLS_FILE = env.get("PROJ_URLS_FILE")
DELAY = 1 # delay in seconds
SHORT_DELAY = 0.5 # short delay
ELEMENT_WAIT_TIME = 5 # wait time in sec for getting elements
MENU_WAIT_TIME = 10 # menu wait time in sec
DEFAULT_WEBDRIVER_WAIT_TIME = 30 # default wait time in sec for webdriver
SHORT_WEBDRIVER_WAIT_TIME = 15 # short wait time in seconds for webdriver
REFRESH_WAIT_TIME = 5 # refresh wait time in sec
MAX_RETRY = 1 # max number of retries for export
# Build absolute path to output folder in current directory
OUTPUT_PATH = os.path.join(os.getcwd(), env.get("OUTPUT_DIR_NAME"))
# Build absolute path to temp download folder in current directory
TEMP_OUTPUT_PATH = os.path.join(OUTPUT_PATH, env.get("TEMP_DIR_NAME"))
# Build export log directory path
EXPORT_LOG_DIR = os.path.join(os.getcwd(), env.get("EXPORT_LOG_DIR_NAME"))
# Build export log messages file for skipped/failed downloads path
# Add the current date and time of script start in the file name
export_log_file = "{}_{}".format(
time.strftime("%d-%m-%Y_%H-%M-%S", time.localtime()), env.get("EXPORT_LOG_NAME")
)
EXPORT_LOG_FILE = os.path.join(EXPORT_LOG_DIR, export_log_file)
# global EXPORT_LOG dict object
EXPORT_LOG = {}
# CREDENTIALS #
USERNAME = env.get("O1_email")
PASSWORD = env.get("O1_password")
# URLS #
BENTLEY_LOGIN_URL = env.get("BENTLEY_LOGIN_URL")
ALL_SYNCHRO_URL = env.get("ALL_SYNCHRO_URL")
PROJ_URL_PLACEHOLDER = env.get("PROJ_URL_PLACEHOLDER")
# Selenium webdriver options #
def get_options(dev=False):
options = webdriver.ChromeOptions()
# run in headless mode if dev is False, else run foreground mode
if dev is False:
options.add_argument("--headless")
# overcome limited resource problems
options.add_argument("--disable-dev-shm-usage")
# set window size
options.add_argument("--window-size=1920,1200")
# avoid granting access to camera and microphone
options.add_argument("--use-fake-ui-for-media-stream")
# disable notifications
options.add_argument("--disable-notifications")
# disable popup blocking
options.add_argument("--disable-popup-blocking")
# disable infobars
options.add_argument("--disable-infobars")
# disable extensions
options.add_argument("--disable-extensions")
# disable crash reporting
options.add_argument("--disable-crash-reporter")
# disable crash uploading list
options.add_argument("--disable-crash-upload")
# disable oopr debug crash dump
options.add_argument("--disable-oopr-debug-crash-dump")
# disable background timer throttling
options.add_argument("--disable-background-timer-throttling")
# disable backgrounding occluded windows
options.add_argument("--disable-backgrounding-occluded-windows")
# disable renderer backgrounding
options.add_argument("--disable-renderer-backgrounding")
# disable logging switch
options.add_experimental_option("excludeSwitches", ["enable-logging"])
# add prefs to change default download directory
options.add_experimental_option(
"prefs",
{
"download.default_directory": TEMP_OUTPUT_PATH,
"profile.default_content_settings.popups": 0,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
},
)
options.add_experimental_option("excludeSwitches", ["enable-logging"])
options.add_argument("--log-level=3")
return options
# Get chrome webdriver options
if env.get("dev_mode").lower() == "true":
OPTIONS = get_options(dev=True)
else:
OPTIONS = get_options()
# Custom Exceptions #
class CustomException(Exception):
"Base class for other exceptions"
def __init__(self, message):
super().__init__(message)
class NavigationError(CustomException):
"Raised when there is an error navigating to a page"
def __init__(self, message):
super().__init__(message)
class CookiesInvalidError(CustomException):
"Raised when cookies are invalid"
def __init__(self, message):
super().__init__(message)
class CookiesFileNotFoundError(CustomException):
"Raised when cookies file is not found"
def __init__(self, message):
super().__init__(message)
class LoginError(CustomException):
"Raised when login fails"
def __init__(self, message):
super().__init__(message)
class ProjectUrlsFileNotFoundError(CustomException):
"Raised when project urls file is not found"
def __init__(self, message):
super().__init__(message)
# All Selenium Exceptions to catch
SELENIUM_ERROR = (
TimeoutException,
WebDriverException,
NoSuchElementException,
StaleElementReferenceException,
ElementNotVisibleException,
)
# Ignored exceptions for WebDriverWait
IGNORED_EXCEPTIONS = (
NoSuchElementException,
StaleElementReferenceException,
ElementNotVisibleException,
ElementClickInterceptedException,
ElementNotInteractableException,
NoSuchAttributeException,
)
# All Exceptions Classes
ALL_ERRORS = SELENIUM_ERROR + (Exception, OSError)
# Get xpaths and css selectors for elements
table_row = env.get("table_row")
table_row_title = env.get("table_row_title")
three_dots = env.get("three_dots")
export_to_pdf = env.get("export_to_pdf")
tippy_box = env.get("tippy_box")
archive_export_pdf = env.get("archive_export_pdf")
select_all_box = env.get("select_all_box")
export_btn_xpath = env.get("export_btn_xpath")
archive_export_excel = env.get("archive_export_excel")
export_excel = env.get("export_excel")
total_forms_item = env.get("total_forms_item")
table_rows = env.get("table_rows")
page_item = env.get("page_item")
active_page_item = env.get("active_page_item")
next_page_item = env.get("next_page_item")
export_modal = env.get("export_modal")
checkboxes_xpath_dict = {
"Comments": env.get("comments_box"),
"Audit trail": env.get("audit_trail_box"),
"Images": env.get("images_box"),
"Export attachments": env.get("export_attachments_box"),
}
# Define a custom event HANDLER for file changes in the downloads folder
class DownloadHandler(FileSystemEventHandler):
def __init__(self, expected_file_name):
super().__init__()
self.expected_file_name = expected_file_name
self.downloaded_file_name = None
self.download_completed = False
def on_created(self, event):
# when download is started, on created will be called
# temp file with .crdownload is created on chrome
LOG.debug("File Created: %s", event.src_path)
def on_modified(self, event):
# when download is completed, on modified will be called
# LOG.debug("File modified: %s", event.src_path)
# check either if file name is same or similar by extension
# account for file name with timestamp by checking if file name
# ends with the same extension
modified_file_name = os.path.basename(event.src_path)
file_name_same = modified_file_name == self.expected_file_name
file_name_similar = modified_file_name.endswith(
os.path.splitext(self.expected_file_name)[1]
)
file_name_match = file_name_same or file_name_similar
ends_with_crdownload = modified_file_name.endswith(".tmp")
unexpected_file = not file_name_match and not ends_with_crdownload
if file_name_match and self.download_completed is False:
LOG.debug(f"Downloaded: {event.src_path}")
LOG.info("Downloaded: {}".format(modified_file_name))
self.download_completed = True
# if file name is similar and not same, set downloaded file name
if file_name_similar and file_name_same is False:
self.downloaded_file_name = modified_file_name
elif unexpected_file and self.download_completed is False:
# edge case for when file name is not same or similar
LOG.debug(f"Downloaded: {event.src_path}")
LOG.info("Downloaded: {}".format(modified_file_name))
self.download_completed = True
self.downloaded_file_name = modified_file_name
HANDLER = colorlog.StreamHandler()
FORMATTER = colorlog.ColoredFormatter(
env.get("log_format"),
datefmt="%d-%m-%Y %H:%M:%S",
reset=True,
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "red,bg_white",
},
secondary_log_colors={},
style="%",
)
FHFORMATTER = formatter = logging.Formatter(
env.get("file_log_format"), datefmt="%a, %d %b %Y %H:%M:%S"
)
HANDLER.setFormatter(FORMATTER)
FH = logging.FileHandler("{}.log".format(os.path.basename(__file__).replace(".py", "")))
FH.setFormatter(FHFORMATTER)
LOG = colorlog.getLogger(__name__)
LOG.addHandler(FH)
LOG.addHandler(HANDLER)
# set log level to DEBUG / INFO / WARNING / ERROR / CRITICAL
if env.get("dev_mode").lower() == "true":
# set log level to debug if dev mode true
LOG.setLevel(colorlog.DEBUG)
else:
LOG.setLevel(colorlog.INFO)
def get_total_forms(total_forms):
"""Returns total number of forms rounded up to nearest whole number"""
# 1 page = 25 forms
return -(-int(total_forms) // 25)
def login_optimus(browser):
"""
Login to Bentley Webapp
Args:
browser (WebDriver): Selenium webdriver object
Raises:
TimeoutException: TimeoutException when element is not found
LoginError: LoginError when login fails
"""
LOG.info("Logging In...")
browser.get(BENTLEY_LOGIN_URL)
try:
# set wait for default webdriver wait time
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
# get email field
email = wait.until(
EC.presence_of_element_located((By.ID, env.get("email_field")))
)
# use action to send keys to email field
action = ActionChains(browser)
action.send_keys_to_element(email, USERNAME).perform()
# set wait for element wait time
wait = WebDriverWait(
browser, ELEMENT_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
# get next btn
next_btn = wait.until(
EC.element_to_be_clickable((By.ID, env.get("sign_in_btn")))
)
action.click(next_btn).perform()
# wait for password field to be present
password = wait.until(
EC.presence_of_element_located((By.ID, env.get("pw_field")))
)
# get password field
action.send_keys_to_element(password, PASSWORD).perform()
# get sign in btn
sign_in_btn = wait.until(
EC.element_to_be_clickable((By.ID, env.get("sign_in_btn")))
)
action.click(sign_in_btn).perform()
# wait for text in div to be present for up to 30s till user enters 2fa
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
wait.until(EC.presence_of_element_located((By.XPATH, env.get("pingid_div"))))
LOG.critical("PingID Authentication Required!")
# wait until 'Change Password' Button is present, user is logged in
wait.until(EC.presence_of_element_located((By.ID, env.get("change_pw_btn"))))
LOG.info("PingID Authenticated!")
# if login was successful, navigate to project page and save cookies
browser.get(ALL_SYNCHRO_URL)
# wait till 'All projects' text is present
wait.until(EC.presence_of_element_located((By.XPATH, env.get("all_proj_div"))))
LOG.info("Login Successful!")
# save cookies to file
save_cookies(browser)
return True
except TimeoutException:
raise LoginError("Login Failed!")
def navigate_to_page(
browser,
url=PROJ_URL_PLACEHOLDER,
wait_condition="//h2[contains(text(), 'My work')]",
):
"""_summary_
Args:
browser (WebDriver): Selenium webdriver object
url (str, optional): Target URL. Defaults to PROJ_URL_PLACEHOLDER.
wait_condition (str, optional): Element to wait for using webdriver.
Defaults to "//h2[contains(text(), 'My work')]".
Make sure this is an element that is present on the page you are
navigating to. If not TimeOut will be raised.
Raises:
TimeoutException: TimeoutException when element is not found
NavigationError: NavigationError when navigation fails
"""
LOG.debug("GET:> {}".format(url))
browser.get(url)
# if browser title is 'Sign in to your account', raise error and relogin
if browser.title == "Choose an Account":
LOG.info("Cookies conflict, restarting script...")
raise CookiesInvalidError("Cookies Conflict!")
else:
# wait for a condition to be present for up to 30s
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
# wait till 'wait_condition' is present
try:
wait.until(EC.presence_of_element_located((By.XPATH, wait_condition)))
LOG.debug("GET:> {} OK!".format(url))
except TimeoutException:
msg = "Wait Timeout! \nTarget: {} \nActual: {}\nCondition: {}".format(
url, browser.current_url, wait_condition
)
raise NavigationError(msg)
def get_proj_form_types(browser):
"""Get list of form types in work project"""
LOG.debug("Getting list of form types in work project...")
# find and click work tab
click_work_tab(browser)
# get form types element list
form_types_elem_list = get_form_types_elem_list(browser)
# generate the form types list
form_types_list = [form_type.text for form_type in form_types_elem_list]
return form_types_list
def click_work_tab(browser):
"""Click work tab in nav bar"""
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
action = ActionChains(browser)
# get the work tab in li element with and title='Work' and click it
work_tab = wait.until(EC.element_to_be_clickable((By.XPATH, env.get("work_tab"))))
action.click(work_tab).perform()
# wait till work projects nav bar container is present
wait.until(EC.presence_of_element_located((By.CLASS_NAME, env.get("work_proj"))))
def get_form_types_elem_list(browser):
"""Get and return form types element list"""
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
form_nav_bar = env.get("form_nav_bar")
form_types = env.get("form_types")
# wait till work project form items in list tab are present
form_types_nav_bar = wait.until(
EC.presence_of_element_located((By.CLASS_NAME, form_nav_bar))
)
form_types_elem_list = form_types_nav_bar.find_elements(By.CLASS_NAME, form_types)
return form_types_elem_list
def refresh_page_form_types(browser):
"""Refresh page and wait for form types nav bar to be present"""
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
form_nav_bar = env.get("form_nav_bar")
try:
LOG.debug("Refreshing page...")
browser.refresh()
# wait for page to refresh
time.sleep(REFRESH_WAIT_TIME)
# click work tab
click_work_tab(browser)
# check for presence of form nav bar
wait.until(EC.presence_of_element_located((By.CLASS_NAME, form_nav_bar)))
LOG.debug("Page refreshed!")
except (TimeoutException, NoSuchElementException):
LOG.warning("Page refresh failed!")
def export_forms_data(browser, form_types_list, proj_folder, proj_name):
"""
Exports all forms dada in project to excel and pdf
Args:
browser (WebDriver): Selenium webdriver object
form_types_list (list): form types list
proj_folder (str): project folder path string
proj_name (str): project name string
"""
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
action = ActionChains(browser)
# strip the form type of any whitespaces
form_types_list = [form_type.strip() for form_type in form_types_list]
# loop through all the list items
ignore_form = ["My work", "Work by items", "CSC for VO Works", "Incident Investigation Report", "Notice of Claims", "Quality Inspection"]
for form_type in form_types_list:
# ignore 'My Work' form type
if form_type in ignore_form:
continue
retry_count = 0
form_found = False
while form_found is False and retry_count <= MAX_RETRY:
LOG.info("Form: {}".format(form_type))
try:
# refresh the form_types_elem_list to get the latest elements
form_types_elem_list = get_form_types_elem_list(browser)
cur_form_type = form_types_elem_list[form_types_list.index(form_type)]
# click on the form type
action.click(cur_form_type).perform()
# wait till form type page is loaded
wait.until(
EC.presence_of_element_located(
(By.XPATH, "//h3[contains(text(), '{}')]".format(form_type))
)
)
# set form_found to True
form_found = True
except (TimeoutException, NoSuchElementException):
LOG.warning(
"{} Form Not Found! Retrying {}/{}".format(
form_type, retry_count + 1, MAX_RETRY
)
)
# refresh page if get form types nav bar is timed out
refresh_page_form_types(browser)
retry_count += 1
if retry_count >= MAX_RETRY:
LOG.warning(
"Skipping... {}, Form Not Found After {} Retry!".format(
form_type, MAX_RETRY
)
)
setup_export_log(proj_name, form_type)
# update export log with form not found error
EXPORT_LOG[proj_name]["forms"][form_type][
"forms_export_error"
] = "Form Not Found!"
continue
else:
# check if form type is archived and/or empty
archive = check_is_archived(browser, form_type)
is_empty = check_is_empty(browser)
if is_empty is False:
# if no empty container, get the forms
setup_export_log(proj_name, form_type)
# create project folder for form type
form_type_folder = os.path.join(proj_folder, form_type)
if not os.path.exists(form_type_folder):
os.makedirs(form_type_folder)
# export forms data to excel
export_forms_excel(
browser, form_type, form_type_folder, proj_name, archive
)
# pdf_form_type_folder = os.path.join(form_type_folder, "PDFs")
pdf_form_type_folder = os.path.join(form_type_folder)
# create pdf folder in form type folder
if not os.path.exists(pdf_form_type_folder):
os.makedirs(pdf_form_type_folder)
# export forms data to pdf
export_forms_pdf(
browser, form_type, pdf_form_type_folder, proj_name, archive
)
else:
LOG.info("No forms in {}".format(form_type))
continue
return True
def check_is_archived(browser, form_type):
"""Check if form type is archived"""
archived_container = env.get("archived_container")
try:
is_archived = WebDriverWait(
browser, ELEMENT_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
).until(EC.presence_of_all_elements_located((By.XPATH, archived_container)))
# if archived container is present, set archive to true
if is_archived:
LOG.info("{} is Archived".format(form_type))
return True
except TimeoutException:
return False
def check_is_empty(browser):
"""Check if form type is empty"""
empty_container = env.get("empty_container")
try:
empty_container = WebDriverWait(
browser, ELEMENT_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
).until(EC.presence_of_all_elements_located((By.XPATH, empty_container)))
# if empty container is present, skip to next form type
if empty_container:
return True
except TimeoutException:
return False
def refresh_page_export(browser, wait_element):
"""Refresh page and wait for page element to be present"""
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
try:
LOG.debug("Refreshing page...")
browser.refresh()
# buffer time for page to refresh and load
time.sleep(REFRESH_WAIT_TIME)
# wait till element is present
wait.until(EC.presence_of_all_elements_located((By.XPATH, wait_element)))
LOG.debug("Page refreshed!")
except (TimeoutException, NoSuchElementException):
LOG.warning("Page refresh failed!")
def export_forms_excel(browser, form_type, target_folder, proj_name, archive=False):
"""
Export all forms data in form type to excel
Args:
browser (Webdriver): Selenium webdriver object
form_type (object): form type object
target_folder (string): target folder to move the exported excel to
"""
excel_file_name = "{}.xlsx".format(form_type)
cur_item = (None, None)
retry_count = 0
is_exported = False
# set wait for default webdriver wait time
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
# loop till export is successful or retry count is more than max retry
while is_exported is False and retry_count <= MAX_RETRY:
try:
cur_item = ("Export all to Excel ", "button")
# wait till table row is present
wait.until(EC.presence_of_element_located((By.XPATH, table_row)))
LOG.info("{} | {} > Export Excel".format(proj_name, form_type))
# export all data to excel main function
do_export_forms_data_excel_main(browser, archive)
# check if excel file is downloaded
download_completed, new_file_name = await_download_complete(excel_file_name)
if download_completed:
if new_file_name:
# rename excel file to new file name if any
excel_file_name = new_file_name
# move exported excel file to project folder
move_file_to_target_folder(target_folder, excel_file_name)
# set is_exported to True
is_exported = True
# set excel_exported to True for current form type
EXPORT_LOG[proj_name]["forms"][form_type]["excel_exported"] = True
else:
LOG.warning("Download failed!")
except TimeoutException:
LOG.warning(not_found_msg(cur_item))
LOG.warning("Export Excel Failed!")
# refresh page if any element is timed out
refresh_page_export(browser, table_row)
if retry_count < MAX_RETRY:
LOG.warning(
"Export Excel > {} | Retry: {}/{}".format(
form_type, retry_count + 1, MAX_RETRY
)
)
# increase retry count by 1
retry_count += 1
# if retry count is more than max retry, skip to export to pdf
if retry_count >= MAX_RETRY:
LOG.warning(
"Export Excel > {} | Max Retry: {} reached!".format(form_type, MAX_RETRY)
)
LOG.warning("Skipping to Export PDF...")
EXPORT_LOG[proj_name]["forms"][form_type]["excel_export_error"] = not_found_msg(
cur_item
)
def do_export_forms_data_excel_main(browser, archive):
"""
Export all forms data in form type to excel main function
Args:
browser (Webdriver): Selenium webdriver object
archive (bool): True if form type is archived. Defaults to False.
"""
# set wait for element wait time
wait = WebDriverWait(
browser, ELEMENT_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
action = ActionChains(browser)
# if form is archived
if archive is True:
cur_item = ("Export all data to Excel", "btn")
export_btn = wait.until(
EC.presence_of_element_located((By.XPATH, archive_export_excel))
)
# use action to click export btn
action.move_to_element(export_btn).click().perform()
LOG.debug(found_msg(cur_item))
# if form is not archived
else:
# click the menu btn
cur_item = ("Menu", "btn")
menu_btn = wait.until(EC.presence_of_element_located((By.XPATH, three_dots)))
action.click(menu_btn).perform()
LOG.debug(found_msg(cur_item))
# click the export to excel btn
cur_item = ("Export all data to Excel", "btn")
# get the export to excel btn
export_excel_btn = wait.until(
EC.element_to_be_clickable((By.XPATH, export_excel))
)
# use action to click export btn
action.move_to_element(export_excel_btn).click().perform()
LOG.debug(found_msg(cur_item))
def export_forms_pdf(browser, form_type, target_folder, proj_name, archive=False):
"""
Export forms in form type to pdf
Args:
browser (Webdriver): Selenium webdriver object
form_type (object): form type object
target_folder (string): target folder to move the exported pdf to
proj_name (string): project name
archive (bool, optional): True if form type is archived.
Defaults to False.
"""
# set wait for short webdriver wait time
wait = WebDriverWait(
browser, SHORT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
try:
# wait and get the total number of forms in the form type
total_forms = wait.until(
EC.presence_of_element_located((By.XPATH, total_forms_item))
)
# split the text to get the total number of forms
total_forms = int(total_forms.text.split("of")[-1].strip())
# get the total number of pages (1 page = 25 forms)
total_pages = get_total_forms(total_forms)
except TimeoutException:
total_pages = 1
rows = browser.find_elements(By.XPATH, table_rows)
total_forms = len(rows)
LOG.debug("{} | {} Total Forms: {}".format(proj_name, form_type, total_forms))
LOG.debug("{} | {} Total Pages: {}".format(proj_name, form_type, total_pages))
# update export log with total forms
EXPORT_LOG[proj_name]["forms"][form_type]["total_forms"] = total_forms
# if total pages is more than 1, export all forms in each page
if total_pages > 1:
multi_page_export_forms_pdf(
browser, form_type, target_folder, proj_name, total_pages, archive
)
# if total pages is 1, export all forms in single page
else:
single_page_export_forms_pdf(
browser, form_type, target_folder, proj_name, total_forms, archive
)
def multi_page_export_forms_pdf(
browser, form_type, target_folder, proj_name, total_pages, archive=False
):
"""Export all forms in each page to pdf"""
cur_item = (None, None)
# if there are more than 1 page, loop through each page
LOG.debug("More than 1 page")
for page in range(1, total_pages + 1):
# set retry count to 0 for each run
retry_count = 0
# set is_exported to False for each run
is_exported = False
LOG.debug(
"{} | {} | Page: {}/{}".format(proj_name, form_type, page, total_pages)
)
# loop till export is successful or retry count is more than max retry
while is_exported is False and retry_count <= MAX_RETRY:
# reset active page number to 0 for each try
active_page_num = 0
# Find the active page element
try:
# try and wait till active page is present
cur_item = ("Active Page", "span")
active_page_num = get_active_page_num(browser)
# print all the page numbers
LOG.debug(
"Current Page: {} | Goto Page: {}".format(active_page_num, page)
)
# If active page is not current page, go next page
if active_page_num != page:
go_next_page(browser)
else:
cur_page_total_forms = len(
browser.find_elements(By.XPATH, table_rows)
)
# if active page is current page, stay on current page
is_exported = stay_on_current_page(
browser,
page,
total_pages,
form_type,
target_folder,
proj_name,
archive,
)
if is_exported is False:
# refresh page if export failed
LOG.warning("Export PDF Failed!")
cur_item = ("Table Rows", "tr")
refresh_page_export(browser, table_rows)
if retry_count < MAX_RETRY:
LOG.warning(
"Export PDF > {} | Page: {}/{} | Retry: {}/{}".format(
form_type,
page,
total_pages,
retry_count + 1,
MAX_RETRY,
)
)
# increase retry count by 1
retry_count += 1
else:
# update export log with total exported forms
EXPORT_LOG[proj_name]["forms"][form_type][
"total_exported_forms"
] += cur_page_total_forms
except (
TimeoutException,
StaleElementReferenceException,
NoSuchElementException,
):
LOG.warning(not_found_msg(cur_item))
if retry_count >= MAX_RETRY:
LOG.warning(
"Export PDF > {} | Page: {}/{} | Max Retry: {} reached!".format(
form_type, page, total_pages, MAX_RETRY
)
)
LOG.warning("Skipping to next page...")
# add page to pdf export error list
error_page_list = EXPORT_LOG[proj_name]["forms"][form_type][
"pdfs_export_error"
]
error_page_list.append({"page": page, "error": not_found_msg(cur_item)})
# if all pages pdf export is successful, set pdfs_exported to True
if EXPORT_LOG[proj_name]["forms"][form_type]["pdfs_export_error"] == []:
EXPORT_LOG[proj_name]["forms"][form_type]["pdfs_exported"] = True
def get_active_page_num(browser):
"""Get the current active page number"""
# try and wait till active page is present
wait = WebDriverWait(
browser, ELEMENT_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
active_page = wait.until(
EC.presence_of_element_located((By.XPATH, active_page_item))
)
if active_page.text:
active_page_num = int(active_page.text)
else:
active_page_num = 0
return active_page_num
def go_next_page(browser):
"""Function to go to next page"""
wait = WebDriverWait(
browser, ELEMENT_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
action = ActionChains(browser)
LOG.debug("Going Next Page")
# get the next page item btn using next page button
cur_item = ("Next Page", "btn")
# wait for next page item btn to be present
next_page_item_btn = wait.until(
EC.presence_of_element_located((By.XPATH, next_page_item))
)
# wait some time before click next page
time.sleep(SHORT_DELAY)
# Click the "Next Page" button
action.click(next_page_item_btn).perform()
LOG.debug(found_msg(cur_item))
def stay_on_current_page(
browser, page, total_pages, form_type, target_folder, proj_name, archive
):
"""Function for stuff to do on current page"""
LOG.debug("Staying on Current Page")
# Continue with exporting forms in current page
is_exported = do_export_forms_pdf_main(
browser, form_type, target_folder, proj_name, page, total_pages, archive
)
return is_exported
def single_page_export_forms_pdf(
browser, form_type, target_folder, proj_name, total_forms, archive=False
):
"""Export all forms in single page to pdf"""
cur_item = (None, None)
# if there is only 1 page, export forms in current page
LOG.debug("Only 1 page")
retry_count = 0
is_exported = False
# Continue with exporting forms in current page
while is_exported is False and retry_count <= MAX_RETRY:
# export all forms in current page
is_exported = do_export_forms_pdf_main(
browser, form_type, target_folder, proj_name, 1, 1, archive
)
if is_exported is False:
try:
# refresh page if export failed
LOG.warning("Export PDF Failed!")
cur_item = ("Table Rows", "tr")
refresh_page_export(browser, table_rows)
if retry_count < MAX_RETRY:
LOG.warning(
"Export PDF > {} | Retry: {}/{}".format(
form_type, retry_count + 1, MAX_RETRY
)
)
# increase retry count by 1
retry_count += 1
except (
TimeoutException,
StaleElementReferenceException,
NoSuchElementException,
):
LOG.warning(not_found_msg(cur_item))
else:
# if export is successful, set pdfs_exported to True
EXPORT_LOG[proj_name]["forms"][form_type]["pdfs_exported"] = True
# update export log with total exported forms
EXPORT_LOG[proj_name]["forms"][form_type][
"total_exported_forms"
] += total_forms
if retry_count >= MAX_RETRY:
LOG.warning(
"Export PDF > {} | Max Retry: {} reached!".format(form_type, MAX_RETRY)
)
LOG.warning("Skipping to next form type...")
# add page to pdfs export error list
error_page_list = EXPORT_LOG[proj_name]["forms"][form_type]["pdfs_export_error"]
error_page_list.append({"page": 1, "error": not_found_msg(cur_item)})
def do_export_forms_pdf_main(
browser, form_type, target_folder, proj_name, page, total_pages, archive=False
):
"""
Function to select all forms in current page and export to pdf
Args:
wait (WebDriverWait): WebDriverWait object
browser (Webdriver): Webdriver object
target_folder (str): str of target folder to move the pdfs zip file to
archive (bool, optional): is form archived?
"""
cur_item = (None, None)
# set wait for default webdriver wait time
wait = WebDriverWait(
browser, DEFAULT_WEBDRIVER_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
action = ActionChains(browser)
# wait till page is loaded
wait.until(EC.presence_of_element_located((By.XPATH, table_row)))
# set wait for element wait time
wait = WebDriverWait(
browser, ELEMENT_WAIT_TIME, ignored_exceptions=IGNORED_EXCEPTIONS
)
LOG.info(
"{} | {} | Export PDF Page: {}/{}".format(
proj_name, form_type, page, total_pages
)
)
try:
# click the select all checkbox
cur_item = ("Select all", "checkbox")
select_all = wait.until(
EC.presence_of_element_located((By.XPATH, select_all_box))
)
action.click(select_all).perform()
LOG.debug(found_msg(cur_item))
time.sleep(SHORT_DELAY) # give time for page to load
# if form is archived
if archive is True:
cur_item = ("Export to PDF", "btn")
export_pdf_btn = wait.until(
EC.presence_of_element_located(
(By.XPATH, "{}".format(archive_export_pdf))
)
)
# use action to click export to pdf btn
action.click(export_pdf_btn).perform()
LOG.debug(found_msg(cur_item))
# if form is not archived
else:
is_tippy_box = False
is_export_pdf_btn = False
# ensure tippy box is present before clicking export to pdf btn
while is_tippy_box is False or is_export_pdf_btn is False:
try:
# click the menu btn
cur_item = ("Menu", "btn")