Skip to content

Automate browser based workflows with AI

fix: re-inject domUtils.js on JS context loss during scraping (#SKY-8202) (#4981)

pedrohsdb8d2b26ce780a
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index f1ccc1dae..e16df2696 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -820,7 +820,7 @@ class ForgeAgent:
task,
step,
sfe.reason
- or "Skyvern failed to load the website. This usually happens when the website is not properly designed, and crashes the browser as a result.",
+ or "Skyvern failed to load the website. The page may have navigated unexpectedly or become unresponsive during analysis.",
browser_state,
)
await self.clean_up_task(
diff --git a/skyvern/webeye/utils/page.py b/skyvern/webeye/utils/page.py
index ac1a15a03..6b0a4b6c6 100644
--- a/skyvern/webeye/utils/page.py
+++ b/skyvern/webeye/utils/page.py
@@ -8,6 +8,7 @@ from typing import Any
import structlog
from PIL import Image
+from playwright._impl._errors import Error as PlaywrightError
from playwright._impl._errors import TimeoutError
from playwright.async_api import ElementHandle, Frame, Page
@@ -248,6 +249,30 @@ class SkyvernFrame:
try:
async with asyncio.timeout(timeout_ms / 1000):
return await frame.evaluate(expression=expression, arg=arg)
+ except PlaywrightError as e:
+ error_msg = str(e)
+ is_context_destroyed = "Execution context was destroyed" in error_msg
+ is_domutils_missing = "ReferenceError" in error_msg and "is not defined" in error_msg
+ if is_context_destroyed or is_domutils_missing:
+ LOG.warning(
+ "JS execution context lost (likely due to page navigation), re-injecting domUtils.js and retrying",
+ expression=expression[:200],
+ error=error_msg[:200],
+ )
+ try:
+ await frame.evaluate(expression=JS_FUNCTION_DEFS)
+ except PlaywrightError:
+ LOG.warning("Re-injection of domUtils.js also failed, page may still be navigating")
+ raise
+ # First call failed fast (context destroyed, not timeout), so a fresh
+ # timeout for the retry is acceptable.
+ try:
+ async with asyncio.timeout(timeout_ms / 1000):
+ return await frame.evaluate(expression=expression, arg=arg)
+ except asyncio.TimeoutError:
+ LOG.exception("Skyvern timed out on retry after JS context re-injection", expression=expression)
+ raise TimeoutError("Skyvern timed out trying to analyze the page")
+ raise
except asyncio.TimeoutError:
LOG.exception("Skyvern timed out trying to analyze the page", expression=expression)
raise TimeoutError("Skyvern timed out trying to analyze the page")