import re import time from selenium.webdriver.common.by import By # "Procedure information" table on a DentaQuest pre-auth detail page, columns in this order: # [expand] | Submitted code | Tooth / arch / quad / surface | Paid code | Total submitted | # Deductible amount | Member responsibility | Plan payment | Provider responsibility | Status | Processing policy # Rows are found by their content (a "D####" code cell followed by money cells), since # the column headers may be rendered apart from the data rows. _ROWS_JS = """ const cellText = (el) => (el.innerText || el.textContent || '').replace(/\\s+/g, ' ').trim(); const out = []; for (const row of document.querySelectorAll('tr, [role="row"]')) { let cells = Array.from(row.querySelectorAll('td, th, [role="cell"], [role="gridcell"]')); if (cells.length === 0) cells = Array.from(row.children); const texts = cells.map(cellText); if (texts.some(t => /^D\\d{4}$/.test(t))) out.push(texts); } return out; """ _CODE_RE = re.compile(r"^D\d{4}$") _MONEY_RE = re.compile(r"^-?\$[\d,]+\.\d{2}$") # Fallback for one line of page text, e.g. # D3320 29 / / LR / D3320 $1,050.00 $0.00 $0.00 $564.00 $486.00 Approved _LINE_RE = re.compile( r"^(D\d{4})\s+(.*?)\s*(D\d{4})\s+" r"(\$[\d,.]+)\s+(\$[\d,.]+)\s+(\$[\d,.]+)\s+(\$[\d,.]+)\s+(\$[\d,.]+)\s+(\S.*)$" ) def parse_row_cells(rows): """Map row cell texts to procedure dicts: submittedCode, toothQuadArchSurface, paidAmount (the "Plan payment" column), status.""" result = [] for cells in rows: code_i = next((i for i, c in enumerate(cells) if _CODE_RE.match(c)), None) if code_i is None: continue rest = cells[code_i + 1:] money = [i for i, c in enumerate(rest) if _MONEY_RE.match(c)] # Total submitted, deductible, member responsibility, plan payment, provider responsibility if len(money) < 5: continue status_i = money[4] + 1 result.append({ "submittedCode": cells[code_i], "toothQuadArchSurface": rest[0] if not _CODE_RE.match(rest[0]) else "", "paidAmount": rest[money[3]], "status": rest[status_i] if status_i < len(rest) else "", }) return result def parse_page_text(text): """Procedure rows from the page's text, one row per line.""" result = [] for line in text.splitlines(): line = re.sub(r"\s+", " ", line).strip() m = _LINE_RE.match(line) if m: result.append({ "submittedCode": m.group(1), "toothQuadArchSurface": m.group(2), "paidAmount": m.group(7), "status": m.group(9), }) return result def extract_preauth_procedures(driver, timeout=10): """Read the procedure lines from the pre-auth detail page. [] when none were found.""" deadline = time.time() + timeout procedures = [] while True: try: procedures = parse_row_cells(driver.execute_script(_ROWS_JS) or []) if not procedures: text = driver.find_element(By.TAG_NAME, "body").get_attribute("innerText") or "" procedures = parse_page_text(text) if procedures: break except Exception as e: print(f"[extract_preauth_procedures] reading page failed: {e}") if time.time() >= deadline: break time.sleep(1) print(f"[extract_preauth_procedures] {len(procedures)} row(s): {procedures}") return procedures