Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
perf(SKY-11332): apply scrape_exclude to get_frame_text (#6770)
LawyZheng0074d7cf0eec
diff --git a/skyvern/webeye/scraper/scraper.py b/skyvern/webeye/scraper/scraper.pyindex 906f461e7..6b7080889 100644--- a/skyvern/webeye/scraper/scraper.py+++ b/skyvern/webeye/scraper/scraper.py@@ -274,10 +274,13 @@ async def scrape_website()-async def get_frame_text(iframe: Frame) -> str:+async def get_frame_text(iframe: Frame, scrape_exclude: ScrapeExcludeFunc | None = None) -> str:"""Get all the visible text in the iframe.:param iframe: Frame instance to get the text from.+ :param scrape_exclude: Optional ``filter_frames``-style predicate; True+ means skip. The top-level caller must pass a starting frame the+ predicate would not itself exclude.:return: All the visible text from the iframe."""js_script = "() => document.body.innerText"@@ -297,6 +300,10 @@ async def get_frame_text(iframe: Frame) -> str:if child_frame.is_detached():continue+ # Skip excluded frames before any CDP probe.+ if scrape_exclude is not None and await scrape_exclude(child_frame.page, child_frame):+ continue+try:child_frame_element = await child_frame.frame_element()except Exception:@@ -310,7 +317,7 @@ async def get_frame_text(iframe: Frame) -> str:if not await child_frame_element.is_visible():continue- text += await get_frame_text(child_frame)+ text += await get_frame_text(child_frame, scrape_exclude)return text@@ -505,7 +512,7 @@ async def scrape_web_unsafe(if not elements and not support_empty_page:raise NoElementFound()- text_content = await get_frame_text(page.main_frame)+ text_content = await get_frame_text(page.main_frame, scrape_exclude)html = ""window_dimension = Nonediff --git a/tests/unit/test_get_frame_text_scrape_exclude.py b/tests/unit/test_get_frame_text_scrape_exclude.pynew file mode 100644index 000000000..a9f64268a--- /dev/null+++ b/tests/unit/test_get_frame_text_scrape_exclude.py@@ -0,0 +1,136 @@+from unittest.mock import AsyncMock, MagicMock, patch+from urllib.parse import urlparse++import pytest++from skyvern.webeye.scraper import scraper+++def _make_frame(url: str, child_frames: list | None = None) -> MagicMock:+ """Build a Playwright-Frame stand-in good enough to drive get_frame_text."""+ frame = MagicMock()+ frame.url = url+ frame.child_frames = child_frames or []+ frame.is_detached = MagicMock(return_value=False)++ element = MagicMock()+ element.is_visible = AsyncMock(return_value=True)+ frame.frame_element = AsyncMock(return_value=element)++ page = MagicMock()+ page.url = "https://example.com/"+ frame.page = page+ return frame+++async def test_get_frame_text_evaluates_main_frame_without_exclude() -> None:+ main = _make_frame("https://example.com/")++ with patch.object(scraper.SkyvernFrame, "evaluate", new=AsyncMock(return_value="hello")):+ text = await scraper.get_frame_text(main)++ assert text == "hello"+++async def test_get_frame_text_default_walks_every_child_for_backwards_compat() -> None:+ child_ad = _make_frame("https://ads.example.invalid/iframe")+ child_real = _make_frame("https://example.com/embed")+ main = _make_frame("https://example.com/", child_frames=[child_ad, child_real])++ visited: list[str] = []++ async def _evaluate(frame, expression):+ visited.append(frame.url)+ return f"text({frame.url})"++ with patch.object(scraper.SkyvernFrame, "evaluate", new=AsyncMock(side_effect=_evaluate)):+ text = await scraper.get_frame_text(main)++ # No predicate => historical behavior: every visible non-detached child is walked.+ assert visited == [+ "https://example.com/",+ "https://ads.example.invalid/iframe",+ "https://example.com/embed",+ ]+ assert "text(https://ads.example.invalid/iframe)" in text+++async def test_get_frame_text_skips_excluded_child_and_its_subtree() -> None:+ grandchild_under_ad = _make_frame("https://ads.example.invalid/grandchild")+ child_ad = _make_frame("https://ads.example.invalid/iframe", child_frames=[grandchild_under_ad])+ child_real = _make_frame("https://example.com/embed")+ main = _make_frame("https://example.com/", child_frames=[child_ad, child_real])++ async def _exclude(page, frame) -> bool:+ hostname = urlparse(frame.url or "").hostname or ""+ return hostname == "ads.example.invalid"++ visited: list[str] = []++ async def _evaluate(frame, expression):+ visited.append(frame.url)+ return f"text({frame.url})"++ with patch.object(scraper.SkyvernFrame, "evaluate", new=AsyncMock(side_effect=_evaluate)):+ text = await scraper.get_frame_text(main, scrape_exclude=_exclude)++ # The excluded child must be dropped *before* recursion, so its grandchild+ # is unreachable too.+ assert visited == [+ "https://example.com/",+ "https://example.com/embed",+ ]+ assert "ads.example.invalid" not in text+ # frame_element() must not be probed on excluded frames either, so we don't+ # pay a CDP round trip for frames we already decided to drop.+ child_ad.frame_element.assert_not_awaited()+++async def test_get_frame_text_keeps_child_when_exclude_returns_false() -> None:+ child_keep = _make_frame("https://other.example.com/iframe")+ main = _make_frame("https://example.com/", child_frames=[child_keep])++ exclude_calls: list[tuple[object, object]] = []++ async def _exclude(page, frame) -> bool:+ exclude_calls.append((page, frame))+ return False++ visited: list[str] = []++ async def _evaluate(frame, expression):+ visited.append(frame.url)+ return f"text({frame.url})"++ with patch.object(scraper.SkyvernFrame, "evaluate", new=AsyncMock(side_effect=_evaluate)):+ await scraper.get_frame_text(main, scrape_exclude=_exclude)++ # The predicate is invoked exactly once with (child_frame.page, child_frame)+ # — matching filter_frames' contract.+ assert len(exclude_calls) == 1+ seen_page, seen_frame = exclude_calls[0]+ assert seen_page is child_keep.page+ assert seen_frame is child_keep+ assert visited == [+ "https://example.com/",+ "https://other.example.com/iframe",+ ]+++async def test_get_frame_text_still_skips_detached_children_before_predicate() -> None:+ detached_child = _make_frame("https://ads.example.invalid/dead")+ detached_child.is_detached = MagicMock(return_value=True)+ main = _make_frame("https://example.com/", child_frames=[detached_child])++ async def _exclude(page, frame) -> bool:+ raise AssertionError("predicate must not run for detached frames")++ with patch.object(scraper.SkyvernFrame, "evaluate", new=AsyncMock(return_value="root")):+ text = await scraper.get_frame_text(main, scrape_exclude=_exclude)++ assert text == "root"