from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import base64 import os import time from selenium_DentaQuest_eligibilityCheckWorker import AutomationDentaQuestEligibilityCheck SUBMITTED_URL = "https://providers.dentaquest.com/pre-auth-claims/submitted/" class AutomationTuftsSCOPreAuthCheck(AutomationDentaQuestEligibilityCheck): """ Tufts SCO (DentaQuest) Check PreAuth. login: same as the eligibility check (persistent browser session, OTP if asked). step1: open Pre-authorizations and Claims > Submitted, clear the date range, search by pre-auth / claim number. step2: open the pre-auth from the result row and save its detail page as a PDF (filed under the patient's Approval folder by the backend). """ def __init__(self, data): super().__init__(data) self.preAuthNumber = str(self.data.get("preAuthNumber") or "").strip() self.claimNumber = str(self.data.get("claimNumber") or "").strip() def step1(self): number = self.preAuthNumber or self.claimNumber if not number: return "ERROR: This record has no pre-auth or claim number to search for." wait = WebDriverWait(self.driver, 30) try: print(f"[TuftsSCO PreAuthCheck step1] Opening {SUBMITTED_URL}") self.driver.get(SUBMITTED_URL) if self._is_maintenance_page(): return "ERROR: DentaQuest server is temporarily unavailable (maintenance). Please try again later." # Clear "Set date range" (defaults to the last 2 weeks) so older preauths are found try: clear_btn = WebDriverWait(self.driver, 15).until(EC.element_to_be_clickable( (By.XPATH, "//button[@aria-label='Clear date range']") )) clear_btn.click() print("[TuftsSCO PreAuthCheck step1] Cleared date range") time.sleep(0.5) except TimeoutException: print("[TuftsSCO PreAuthCheck step1] No date range to clear") number_input = wait.until(EC.element_to_be_clickable( (By.XPATH, "//input[@data-testid='claims-search_claim-number_input']") )) number_input.click() number_input.send_keys(Keys.CONTROL, "a") number_input.send_keys(Keys.DELETE) number_input.send_keys(number) print(f"[TuftsSCO PreAuthCheck step1] Entered number: {number}") search_btn = wait.until(EC.element_to_be_clickable( (By.XPATH, "//button[@data-testid='claims-search_submit-btn']") )) search_btn.click() print("[TuftsSCO PreAuthCheck step1] Clicked Search") return "SUCCESS" except TimeoutException: return "ERROR: Pre-auth/claim search form not found on the Submitted page." except Exception as e: return f"ERROR: Pre-auth search failed: {e}" def step2(self): number = self.preAuthNumber or self.claimNumber try: # 1) Wait for the result row and click the pre-auth number in its first column try: id_link = WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable( (By.XPATH, f"//tbody//tr[1]/*[1]//*[self::a or self::button][contains(normalize-space(.), '{number}')]") )) except TimeoutException: body = self.driver.find_element(By.TAG_NAME, "body").text if "0 records found" in body or "no records" in body.lower() or "no results" in body.lower(): return {"status": "error", "message": f"Tufts SCO found no pre-auth/claim #{number}."} return {"status": "error", "message": f"Search result for #{number} did not load."} # Status column of the result row (e.g. "Approved"), read before leaving the page status = "" try: headers = [h.text.strip().lower() for h in self.driver.find_elements(By.XPATH, "//thead//th")] cells = self.driver.find_elements(By.XPATH, "//tbody//tr[1]/td") if "status" in headers and headers.index("status") < len(cells): status = cells[headers.index("status")].text.strip() except Exception: pass print(f"[TuftsSCO PreAuthCheck step2] Status: {status!r}") id_link.click() print(f"[TuftsSCO PreAuthCheck step2] Opened #{number}") # 2) Wait for the detail page ("Pre-authorization for ") try: WebDriverWait(self.driver, 30).until(EC.presence_of_element_located( (By.XPATH, f"//h1[contains(., '{number}')] | //*[self::h1 or self::h2][contains(., 'for ')][contains(., '{number}')]") )) WebDriverWait(self.driver, 30).until(EC.presence_of_element_located( (By.XPATH, "//*[contains(normalize-space(.), 'Procedure information')]") )) except TimeoutException: return {"status": "error", "message": f"Detail page for #{number} did not load."} time.sleep(2) # let the procedure table finish rendering # 3) The page's Print button opens Chrome's print dialog, which Selenium can't drive; # printing to PDF through DevTools produces the same printable page. pdf = self.driver.execute_cdp_cmd("Page.printToPDF", { "printBackground": True, "paperWidth": 8.5, "paperHeight": 11, "marginTop": 0.4, "marginBottom": 0.4, "marginLeft": 0.4, "marginRight": 0.4, }) pdf_path = os.path.join( self.download_dir, f"tuftssco_preauth_approval_{number}_{int(time.time())}.pdf" ) with open(pdf_path, "wb") as f: f.write(base64.b64decode(pdf.get("data", ""))) print(f"[TuftsSCO PreAuthCheck step2] PDF saved: {pdf_path}") # Leave the browser idle so the session stays logged in for the next run try: self.driver.get("about:blank") self.driver.minimize_window() except Exception: pass return { "status": "success", "message": f"Tufts SCO pre-auth #{number}" + (f": {status}." if status else "."), "preAuthNumber": number, "preAuthStatus": status, "approval_pdf_path": pdf_path, } except Exception as e: return {"status": "error", "message": f"Tufts SCO pre-auth check failed: {e}"}