Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Resolve ambiguous typing targets before input (SKY-12707) (#7674)
Aaron Perez8fb640363ba0
diff --git a/skyvern/webeye/utils/dom.py b/skyvern/webeye/utils/dom.pyindex fe99afe73..722bdba76 100644--- a/skyvern/webeye/utils/dom.py+++ b/skyvern/webeye/utils/dom.py@@ -980,23 +980,30 @@ class SkyvernElement:async def refresh_locator_if_stale(self) -> None:"""Re-resolve via the tag-name xpath (same fallback as get_skyvern_element_by_id)- when the scraped css selector no longer matches — a page mutation replaced the node- and the stamped unique-id attribute went with it. Best-effort: swaps the locator- only on an unambiguous xpath match; a detach can be transient (mid re-render), so- anything else falls through to Playwright's own auto-wait instead of failing fast."""- if await self._safe_match_count(self.get_locator()) > 0:+ when the scraped css selector is missing or ambiguous after a page mutation.+ Best-effort for missing targets; ambiguous targets must resolve uniquely or fail."""+ match_count = await self._safe_match_count(self.get_locator())+ if match_count == 1:returnxpath: str | None = self.get_element_dict().get("xpath")- if not xpath:- return- fresh_locator = self.get_frame().locator(f"xpath={xpath}")- if await self._safe_match_count(fresh_locator) == 1:- LOG.info(- "Re-resolved stale element locator by xpath",+ if xpath:+ fresh_locator = self.get_frame().locator(f"xpath={xpath}")+ if await self._safe_match_count(fresh_locator) == 1:+ LOG.info(+ "Re-resolved stale or ambiguous element locator by xpath",+ element_id=self.get_id(),+ xpath=xpath,+ previous_match_count=match_count,+ )+ self.locator = fresh_locator+ return++ if match_count > 1:+ raise MultipleElementsFound(+ num=match_count,+ selector=str(self.get_locator()),element_id=self.get_id(),- xpath=xpath,)- self.locator = fresh_locatorasync def _safe_match_count(self, locator: Locator) -> int:# count() itself raises when the locator's frame detached; that means 0 matchesdiff --git a/tests/unit/test_stale_context_guards.py b/tests/unit/test_stale_context_guards.pyindex b00e9fc22..fa8e06f0f 100644--- a/tests/unit/test_stale_context_guards.py+++ b/tests/unit/test_stale_context_guards.py@@ -6,7 +6,7 @@ import pytestfrom playwright.async_api import Error as PlaywrightErrorfrom playwright.async_api import TimeoutError as PlaywrightTimeoutError-from skyvern.exceptions import MissingElement+from skyvern.exceptions import MissingElement, MultipleElementsFoundfrom skyvern.webeye.utils.dom import SkyvernElement, is_element_detached_error, resolve_locator_DETACHED_ERROR = "ElementHandle.content_frame: Element is not attached to the DOM"@@ -102,9 +102,45 @@ async def test_input_sequentially_reresolves_stale_locator_by_xpath(monkeypatch:frame.locator.assert_called_once_with("xpath=//form/input")assert element.get_locator() is fresh_locator+ assert typed_with.await_args is not Noneassert typed_with.await_args.args[0] is fresh_locator+async def test_input_sequentially_reresolves_ambiguous_locator_by_xpath(monkeypatch: pytest.MonkeyPatch) -> None:+ fresh_locator = _locator_with_counts(1)+ frame = MagicMock()+ frame.locator.return_value = fresh_locator+ element = _make_element(_locator_with_counts(16), frame=frame, xpath="//div/pre[3]")++ typed_with = AsyncMock()+ monkeypatch.setattr("skyvern.webeye.actions.handler_utils.input_sequentially", typed_with)+ await element.input_sequentially("hello")++ frame.locator.assert_called_once_with("xpath=//div/pre[3]")+ assert element.get_locator() is fresh_locator+ assert typed_with.await_args is not None+ assert typed_with.await_args.args[0] is fresh_locator+++async def test_input_sequentially_rejects_ambiguous_locator_without_unique_xpath(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ ambiguous_locator = _locator_with_counts(16)+ frame = MagicMock()+ frame.locator.return_value = _locator_with_counts(2)+ element = _make_element(ambiguous_locator, frame=frame, xpath="//div/pre")++ typed_with = AsyncMock()+ monkeypatch.setattr("skyvern.webeye.actions.handler_utils.input_sequentially", typed_with)++ with pytest.raises(MultipleElementsFound):+ await element.input_sequentially("hello")++ typed_with.assert_not_awaited()++@pytest.mark.asyncioasync def test_input_sequentially_keeps_locator_on_ambiguous_xpath(monkeypatch: pytest.MonkeyPatch) -> None:stale_locator = _locator_with_counts(0)