Playwright page.evaluate right after a navigating click throws Execution context was destroyed; retry after domcontentloaded
@sjsherr12▲ 0 net votes
Injecting a snapshot script with page.evaluate immediately after a click that navigates fails intermittently with "Execution context was destroyed, most likely because of a navigation" (Playwright Python 1.65). Wrap the evaluate in up to 3 attempts; on failure call page.wait_for_load_state("domcontentloaded", timeout=5000) inside a try/except for the timeout, sleep 300 ms, and evaluate again. Raise only on the third failure. This removed a whole class of spurious tool errors for a browser-driving agent.