def check_page_loaded(driver, page_load_xpath): """ :param page_load_xpath: Xpath of Element that is present for page to be considered fully loaded :return: """ start_time = time.time() current_page = driver.current_url print_log(f"LOG INFO: Waiting for page to load: {current_page}") # BEFORE CHECKING FOR TRACKING NUMBERS ON PAGE. CHECK IF PAGE IS LOADED pageLoadComplete = find_presence_of_element(driver, page_load_xpath, time_delay=18) if pageLoadComplete is True: print_log( f"LOG SUCCESS: Page loading complete after {time.time() - start_time} seconds" ) else: print_log("LOG WARNING: Page did not load on first try. Waiting") pageLoadComplete = find_presence_of_element(driver, page_load_xpath, time_delay=18) if pageLoadComplete is True: print_log( f"LOG SUCCESS: Page fully loaded after {time.time() - start_time} seconds" ) else: print_log( f"LOG ERROR: Page did not load completely. Total wait time: {time.time() - start_time} seconds" ) return pageLoadComplete
def clearPauseFile(): checkFile = path.exists('pause.txt') if checkFile is True: print_log("LOG INFO: Clearing pause file") os.remove("pause.txt") return
def check_login_issues(self): current_page = self.driver.current_url if "/myca/logon/us/action/LogLogonHandler" in current_page: msg = f"LOG ERROR: AMEX Login information incorrect for {self.user_id}. From File: {os.path.basename(__file__)}" print_log(f"{msg}") send_slack_notice(response=msg) return True return False
def getHtmlElementObjectAsText(driver, xpath, time_delay=3.0): try: elementObject = WebDriverWait(driver, time_delay).until( EC.presence_of_element_located((By.XPATH, xpath))) elementObjectText = elementObject.get_attribute("outerHTML") print_log(f"LOG INFO: Found html element at {elementObject}") except: return None return elementObjectText
def find_presence_of_element(driver, xpath, time_delay=3.0): check_stop_file() try: element = WebDriverWait(driver, time_delay).until( EC.presence_of_element_located((By.XPATH, xpath))) print_log(f"\nLOG INFO: Element {element} at {xpath} present") except: # traceback.print_exc() print_log(f"\nLOG ERROR: Error finding element given by xpath {xpath}") return False return True
def getElementAttributeAsText(driver, xpath, attribute_name: str, time_delay=3.0): try: elementObject = WebDriverWait(driver, time_delay).until( EC.presence_of_element_located((By.XPATH, xpath))) elementObjectAttributeText = elementObject.get_attribute( attribute_name) except: print_log( f"\nLOG ERROR: Error getting text for the element given by xpath {xpath} or no attribute with name {attribute_name}" ) return None return elementObjectAttributeText
def get_element_text(driver, xpath, time_delay=3.0): check_stop_file() try: element = WebDriverWait(driver, time_delay).until( EC.presence_of_element_located((By.XPATH, xpath))) element_text = element.text print_log(f"\nLOG INFO: Text found for element at {xpath}") except: # traceback.print_exc() print_log( f"\nLOG ERROR: Errorgetting text for the element given by xpath {xpath}" ) script = "return document.getElementById('hidden_div').innerHTML" element_text = None return element_text
def click_element(driver, xpath, time_delay=1.0, pause_after_action=0.01): check_stop_file() try: element = WebDriverWait(driver, time_delay).until( EC.element_to_be_clickable((By.XPATH, xpath))) sleepTime = time_delay / 2 time.sleep(sleepTime) element.click() print_log(f"\nLOG INFO: Element at {xpath} successfully clicked") time.sleep(pause_after_action) except: traceback.print_exc() print_log( f"\nLOG ERROR: Error clicking element given by xpath {xpath}") return False return True
def sendReturnKey(driver, xpath, time_delay=3.0, pause_after_action=0.01): try: empty_field = WebDriverWait(driver, time_delay).until( EC.element_to_be_clickable((By.XPATH, xpath))) sleepTime = time_delay / 2 time.sleep(sleepTime) empty_field.send_keys(Keys.ENTER) print_log(f"\nLOG INFO: ENTER Key sent into field at {xpath}") except: # traceback.print_exc() print_log( f"\nLOG ERROR: Error entering value for the element given by xpath {xpath}" ) return False time.sleep(pause_after_action) return True
def enter_field_value(driver, xpath, value, time_delay=3.0, pause_after_action=0.01): check_stop_file() try: empty_field = WebDriverWait(driver, time_delay).until( EC.element_to_be_clickable((By.XPATH, xpath))) sleepTime = time_delay / 2 time.sleep(sleepTime) empty_field.click() empty_field.clear() empty_field.send_keys(str(value)) print_log(f"\nLOG INFO: Entered value into field at {xpath}") except: try: empty_field = WebDriverWait(driver, time_delay).until( EC.element_to_be_clickable((By.XPATH, xpath))) sleepTime = time_delay / 2 time.sleep(sleepTime) empty_field.send_keys(str(value)) print_log(f"\nLOG INFO: Entered value into field at {xpath}") except: #traceback.print_exc() print_log( f"\nLOG ERROR: Error entering value for the element given by xpath {xpath}" ) return False time.sleep(pause_after_action) return True
def check_stop_file(): checkFile = path.exists('pause.txt') while checkFile is True: print_log("LOG WARN: Pause file present. Pausing script for 5 seconds") timer = 0 while timer < 6: print_log(f"LOG INFO; Waiting: {timer}") time.sleep(1) timer += 1 print_log("LOG INFO: Checking if pause file is present") checkFile = path.exists('pause.txt') print_log("LOG INFO: No pause file.Continuing script") return
def show_all_cards_info(self): for card_object in self.list_of_user_cards_objects: print_log(card_object)
def scrape_all_card_info(self): current_page = self.driver.current_url if current_page != DASHBOARD_PAGE_URL: go_home = click_element(self.driver, xpath=HOME_BUTTON) if self.number_of_cards is None: self.get_num_cards() click_element(self.driver, xpath=CARD_EXPANDER) click_element(self.driver, xpath=VIEW_ALL_BUTTON) for card_idx in range(1, self.number_of_cards + 1): current_card_xpath = f"{CARD_ELEMENTS}[{card_idx}]" card_text = get_element_text(self.driver, current_card_xpath, time_delay=5) print_log(f"\n{card_text}\n") # GET SOME DETAILS FROM CARD SELECTOR DROP DOWN HERE try: card_number = CARD_NUMBER_REGEX.search(card_text).group( 1).strip() print_log(f'LOG SUCCESS: Card Number: {card_number}') except: print_log(f"LOG ERROR:Error Getting card number") card_number = None try: card_name = CARD_NAME_REGEX.search(card_text).group(1).strip() print_log(f"LOG SUCCESS: Card Name: {card_name}") except: print_log(f"LOG ERROR: Error getting card name") card_name = None if card_name: if card_name == "Personal Savings": continue card_selected_details = CardInfo(card_number=card_number, user_id=self.user_id) card_selected_details.card_name = card_name # CLICK ON CARD FROM CARD SELECTOR DROPDOWN HERE click_element(self.driver, current_card_xpath, time_delay=4) # CHECK IF PAGE IS LOADED page_loaded = check_page_loaded( self.driver, page_load_xpath=CARD_ACTIVITY_CONTAINER) if not page_loaded: go_home = click_element(self.driver, xpath=HOME_BUTTON) page_loaded = check_page_loaded( self.driver, page_load_xpath=CARD_ACTIVITY_CONTAINER) # ON PAGE FOR SPECIFIC CARD FROM HERE card_activity_text = get_element_text( self.driver, xpath=CARD_ACTIVITY_CONTAINER) print_log(f"\n{card_activity_text}\n") try: closing_date = CLOSING_DATE_REGEX.search( card_activity_text).group(1).strip() print_log(f"LOG SUCCESS: Closing date {closing_date}") except: print_log(f"LOG ERROR: Error getting closing date") closing_date = None date = date_converter(closing_date) card_selected_details.closing_date = date card_balances_text = get_element_text(self.driver, xpath=CARD_BALANCE_CONTAINER) print_log(f"\n{card_balances_text}\n") # GET PAYMENT AMOUNT DUE try: payment_due_amount = PAYMENT_DUE_REGEX.search( card_balances_text).group(1).strip() payment_due_amount = payment_due_amount.replace(",", "") payment_due_amount = float(payment_due_amount) print_log(f"LOG SUCCESS: Payment Due: {payment_due_amount}") except: print_log(f"LOG ERROR: No Payment due amount") payment_due_amount = 0 card_selected_details.payment_due_amount = payment_due_amount # GET STATEMENT BALANCE try: statement_balance = STATEMENT_BALANCE_REGEX.search( card_balances_text).group(1).strip() statement_balance = statement_balance.replace(",", "") statement_balance = float(statement_balance) print_log( f"LOG SUCCESS: Statement balance: {statement_balance}") except: print_log(f"LOG ERROR: No statement balance") statement_balance = 0 if payment_due_amount > 0 and statement_balance <= 0: statement_balance = payment_due_amount card_selected_details.statement_balance = statement_balance # GET PREVIOUS ACTIVITY (IF ANY) try: previous_activity = PREVIOUS_ACTIVITY_REGEX.search( card_balances_text).group(1).strip() previous_activity = previous_activity.replace(",", "") previous_activity = float(previous_activity) print_log( f"LOG SUCCESS: Previous Activity: {previous_activity}") except: print_log(f"LOG ERROR: No previous activity displayed") previous_activity = None card_selected_details.previous_activity = previous_activity # GET STANDARD BALANCE IF ANY AND UPDATE STATEMENT BALANCE WITH STANDARD BALANCE AMOUNT IF STATEMENT BALANCE IS NOT AVAILABLE try: standard_balance = STANDARD_BALANCE_REGEX.search( card_balances_text).group(1).strip() standard_balance = standard_balance.replace(",", "") standard_balance = float(standard_balance) print_log( f"LOG SUCCESS: Standard Balance found: {standard_balance}") except: print_log( f"LOG INFO: No standard balance present for this account") standard_balance = None if standard_balance: if standard_balance > statement_balance: statement_balance = standard_balance card_selected_details.statement_balance = statement_balance if payment_due_amount < statement_balance: payment_due_amount = statement_balance card_selected_details.payment_due_amount = payment_due_amount # GET AVAILABLE CREDIT AMOUNT try: available_credit = AVAILABLE_CREDIT_REGEX.search( card_balances_text).group(2).strip() available_credit = available_credit.replace(",", "") available_credit = float(available_credit) print_log(f"LOG SUCCESS: Available credit: {available_credit}") except: try: available_credit = NO_PRESET_LIMIT_REGEX.search( card_balances_text).group(1).strip() print_log( f"LOG SUCCESS: Available credit: {available_credit}") except: print_log(f"LOG ERROR: Error getting available credit") available_credit = None card_selected_details.available_credit = available_credit # GET PAYMENT DUE DATE try: payment_due_date = PAYMENT_DUE_DATE_REGEX.search( card_balances_text).group(1).strip() print_log(f'LOG SUCCESS: Payment due date {payment_due_date}') except: try: payment_due_date = PAYMENT_DUE_DATE_REGEX_2.search( card_balances_text).group(1).strip() print_log( f'LOG SUCCESS: Payment due date {payment_due_date}') except: print_log(f"LOG ERROR: Error getting payment due date") payment_due_date = None if payment_due_date: payment_due_date = date_converter(payment_due_date) card_selected_details.payment_due_date = payment_due_date # GET TOTAL BALANCE try: total_balance = TOTAL_BALANCE_REGEX.search( card_balances_text).group(1).strip() total_balance = total_balance.replace(",", "") total_balance = float(total_balance) print_log(f"LOG SUCCESS: Total Balance: {total_balance}") except: print_log(f"LOG ERROR: Error getting total balance") total_balance = 0 card_selected_details.total_balance = total_balance card_selected_details.update_card_identifier() # ADD CURRENT CARD TO LIST OF CARDS FOR THIS USER ID self.list_of_user_cards_objects.append(card_selected_details) # click_element(self.driver, current_card_xpath) click_element(self.driver, xpath=CARD_EXPANDER)
def login_user(self): if not self.driver: self.set_driver() self.driver.get(LOGIN_URL) enter_user_id = enter_field_value(self.driver, USER_ID_FIELD, self.user_id) if enter_user_id is True: print_log(f"LOG INFO: User Id entered successfully") else: print_log(f"LOG ERROR: Error Entering user id in field") enter_password = enter_field_value(self.driver, PASSWORD_FIELD, self.password) if enter_password is True: print_log(f"LOG INFO: Password entered successfully") else: print_log(f"LOG ERROR: Error entering password in field") enter_submit = click_element(self.driver, SUBMIT_FIELD) if enter_submit is True: print_log(f"LOG INFO: Submit clicked successfully") else: print_log(f"LOG ERROR: Error clicking submit button") # AFTER CLICKING SUBMIT, NEXT PAGE SEEN SHOULD BE THE DASHBOARD SCREEN page_loaded = check_page_loaded(self.driver, CARD_ACTIVITY_CONTAINER) if not page_loaded: go_home = click_element(self.driver, xpath=HOME_BUTTON) page_loaded = check_page_loaded(self.driver, CARD_ACTIVITY_CONTAINER) if not page_loaded: print_log(f"LOG WARN: Could not load Amex Home Page!!")