From 441438070e4eeca3b9fe4584468aaa3164d68e76 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Tue, 7 Apr 2026 23:40:26 +0200 Subject: [PATCH] improve export by waiting for invoice page --- downloader.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/downloader.py b/downloader.py index 13dfbb6..cc83603 100644 --- a/downloader.py +++ b/downloader.py @@ -266,27 +266,32 @@ def export_invoice_pdf( billing_url: str = BILLING_URL, ) -> tuple[Page, Path]: invoice_page = None - if invoice.get("open_via_popup") and invoice.get("reference_id"): + if invoice.get("reference_id"): page = _goto_with_retry(context, page, billing_url) selector = f"a[data-reference-object-id='{invoice['reference_id']}']" anchor = page.locator(selector).first - if anchor.count() == 0: - try: - with page.expect_popup() as popup_info: - page.evaluate("printPayment", invoice["reference_id"]) - invoice_page = popup_info.value - invoice_page.wait_for_load_state("domcontentloaded") - except Exception: - raise RuntimeError(f"Invoice link not found for reference id {invoice['reference_id']}") - else: + if anchor.count() > 0: anchor.scroll_into_view_if_needed() with page.expect_popup() as popup_info: anchor.click() invoice_page = popup_info.value - invoice_page.wait_for_load_state("domcontentloaded") + else: + with page.expect_popup() as popup_info: + page.evaluate("printPayment", invoice["reference_id"]) + invoice_page = popup_info.value + + invoice_page.wait_for_load_state("domcontentloaded") + invoice_page.wait_for_url("**/billing_print_invoice.htm**", timeout=30000) else: page = _goto_with_retry(context, page, invoice["url"]) invoice_page = page + invoice_page.wait_for_load_state("domcontentloaded") + if "billing_print_invoice.htm" not in invoice_page.url: + with page.expect_popup() as popup_info: + page.evaluate("printPayment", invoice.get("reference_id")) + invoice_page = popup_info.value + invoice_page.wait_for_load_state("domcontentloaded") + invoice_page.wait_for_url("**/billing_print_invoice.htm**", timeout=30000) time.sleep(1)