From 73687479460aff950606dafce1cc63e55eaf149b Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Thu, 5 Mar 2026 10:35:27 -0500 Subject: [PATCH] perf(atcoder): bail out early from `_solve_turnstile` when no iframe present Problem: `_solve_turnstile` looped 6 times with ~20s per iteration (15s bounding_box timeout + 5s wait_for_function) when no Turnstile iframe existed on the page, causing a 120-second delay on pages that don't require Turnstile verification. Solution: check for existing token and iframe presence before entering the retry loop. `iframe_loc.count()` returns immediately when no matching elements exist, avoiding the expensive timeout cascade. --- scrapers/atcoder.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/scrapers/atcoder.py b/scrapers/atcoder.py index 16eba40..45a2195 100644 --- a/scrapers/atcoder.py +++ b/scrapers/atcoder.py @@ -246,14 +246,14 @@ _TURNSTILE_JS = "() => { const el = document.querySelector('[name=\"cf-turnstile def _solve_turnstile(page) -> None: + if page.evaluate(_TURNSTILE_JS): + return + iframe_loc = page.locator('iframe[src*="challenges.cloudflare.com"]') + if not iframe_loc.count(): + return for _ in range(6): - has_token = page.evaluate(_TURNSTILE_JS) - if has_token: - return try: - box = page.locator( - 'iframe[src*="challenges.cloudflare.com"]' - ).first.bounding_box() + box = iframe_loc.first.bounding_box() if box: page.mouse.click( box["x"] + box["width"] * 0.15,