feat: CCA multi-record popup — save popup as PDF and read green/red status badge

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-24 12:20:40 -04:00
co-authored by Claude Sonnet 5
parent eb731dcbd6
commit 90f587ac9d
@@ -482,6 +482,40 @@ class AutomationCCAEligibilityCheck:
print(f"[CCA step1] Exception: {e}")
return f"ERROR: step1 failed: {e}"
def _capture_popup_pdf(self):
"""Screenshot the visible popup and wrap it in a PDF (no extra deps)."""
try:
shot = self.driver.execute_cdp_cmd("Page.captureScreenshot", {"format": "png"})
png_b64 = shot.get("data", "")
if not png_b64:
return ""
original_window = self.driver.current_window_handle
self.driver.execute_script("window.open('about:blank','_blank');")
self.driver.switch_to.window(self.driver.window_handles[-1])
html = ("<html><body style='margin:0'><img style='width:100%' "
f"src='data:image/png;base64,{png_b64}'></body></html>")
self.driver.get("data:text/html;base64," + base64.b64encode(html.encode()).decode())
time.sleep(1)
res = self.driver.execute_cdp_cmd("Page.printToPDF", {
"printBackground": True,
"paperWidth": 11,
"paperHeight": 8.5,
"marginTop": 0.2, "marginBottom": 0.2,
"marginLeft": 0.2, "marginRight": 0.2,
})
self.driver.close()
self.driver.switch_to.window(original_window)
data = res.get("data", "")
print(f"[CCA step2] Popup PDF, b64 len={len(data)}")
return data
except Exception as e:
print(f"[CCA step2] Popup capture failed: {e}")
try:
self.driver.switch_to.window(self.driver.window_handles[0])
except Exception:
pass
return ""
def step2(self):
"""
Extract all patient information from the result popup,
@@ -508,6 +542,29 @@ class AutomationCCAEligibilityCheck:
eligibility = "Eligible"
elif "not eligible" in body_text.lower() or "ineligible" in body_text.lower():
eligibility = "Not Eligible"
else:
# Popup rows show a status badge: green = active, red = not active
try:
badge = self.driver.execute_script("""
const out = [];
document.querySelectorAll('span,div,label').forEach(el => {
if (el.children.length === 0 && /^\\s*(not\\s+)?(eligible|ineligible|inactive)\\s*$/i.test(el.textContent)) {
if (el.offsetParent === null) return;
const m = getComputedStyle(el).backgroundColor.match(/\\d+/g) || [];
out.push({t: el.textContent.trim(), r: +m[0], g: +m[1], b: +m[2]});
}
});
return out;
""") or []
for b in badge:
if b["g"] > b["r"] and b["g"] > b["b"]:
eligibility = "Eligible"
break
if b["r"] > b["g"] and b["r"] > b["b"]:
eligibility = "Not Eligible"
print(f"[CCA step2] Status badges: {badge} -> {eligibility}")
except Exception as e:
print(f"[CCA step2] Badge check failed: {e}")
# --- Patient name ---
for sel in [
@@ -643,15 +700,18 @@ class AutomationCCAEligibilityCheck:
continue
if not view_report_clicked:
print("[CCA step2] 'View Eligibility Report' button not found")
raise Exception("View Eligibility Report button not found")
print("[CCA step2] 'View Eligibility Report' button not found; "
"capturing the popup itself")
pdfBase64 = self._capture_popup_pdf()
if not pdfBase64:
raise Exception("View Eligibility Report button not found")
# Wait for download to start
time.sleep(3)
# Check for downloaded file (this site downloads rather than opens in-tab)
pdf_path = None
for i in range(15):
for i in range(0 if pdfBase64 else 15):
time.sleep(1)
current_files = set(glob.glob(os.path.join(self.download_dir, "*")))
new_files = current_files - existing_files
@@ -662,7 +722,9 @@ class AutomationCCAEligibilityCheck:
print(f"[CCA step2] PDF downloaded: {pdf_path}")
break
if pdf_path and os.path.exists(pdf_path):
if pdfBase64:
pass
elif pdf_path and os.path.exists(pdf_path):
with open(pdf_path, "rb") as f:
pdfBase64 = base64.b64encode(f.read()).decode()
print(f"[CCA step2] PDF from download: {os.path.basename(pdf_path)} "