Skip to content

Automate browser based workflows with AI

fix(SKY-12013): return missing dropdown target failures (#7195)

Aaron Perez30674711a714
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index 7438c5957..94918d742 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -6831,6 +6831,10 @@ async def select_from_dropdown(
return single_select_result
+def _no_element_matched_failure(value: str, reason: str) -> ActionFailure:
+ return ActionFailure(NoElementMatchedForTargetOption(target=value, reason=reason))
+
+
@traced(name="skyvern.agent.dropdown.select_by_value")
async def select_from_dropdown_by_value(
value: str,
@@ -6864,7 +6868,7 @@ async def select_from_dropdown_by_value(
)
if not dropdown_menu_element:
- raise NoElementMatchedForTargetOption(target=value, reason="No value matched")
+ return _no_element_matched_failure(value=value, reason="No value matched")
potential_scrollable_element = await try_to_find_potential_scrollable_element(
skyvern_element=dropdown_menu_element,
@@ -6873,8 +6877,9 @@ async def select_from_dropdown_by_value(
step=step,
)
if not await skyvern_frame.get_element_scrollable(await potential_scrollable_element.get_element_handler()):
- raise NoElementMatchedForTargetOption(
- target=value, reason="No value matched and element can't scroll to find more options"
+ return _no_element_matched_failure(
+ value=value,
+ reason="No value matched and element can't scroll to find more options",
)
selected: bool = False
@@ -6909,7 +6914,7 @@ async def select_from_dropdown_by_value(
if selected:
return ActionSuccess()
- raise NoElementMatchedForTargetOption(target=value, reason="No value matched after scrolling")
+ return _no_element_matched_failure(value=value, reason="No value matched after scrolling")
async def locate_dropdown_menu(
diff --git a/tests/unit/test_no_available_option.py b/tests/unit/test_no_available_option.py
index 3aadaef09..c353c1aa6 100644
--- a/tests/unit/test_no_available_option.py
+++ b/tests/unit/test_no_available_option.py
@@ -10,6 +10,7 @@ import pytest
from skyvern.exceptions import (
NoAvailableOptionFoundForCustomSelection,
+ NoElementMatchedForTargetOption,
NoIncrementalElementFoundForCustomSelection,
)
from skyvern.forge.agent_functions import AgentFunction
@@ -102,6 +103,17 @@ class _FakeIncrementalScrapePage:
return self._element_trees.pop(0)
+class _FakeValueFallbackScrapePage:
+ def __init__(self) -> None:
+ self.get_incremental_element_tree = AsyncMock(return_value=[])
+ self.select_one_element_by_value = AsyncMock(return_value=None)
+
+
+class _FakeDropdownMenuElement:
+ def __init__(self) -> None:
+ self.get_element_handler = AsyncMock(return_value=MagicMock())
+
+
def _task() -> object:
now = datetime.now(UTC)
organization = make_organization(now)
@@ -1203,3 +1215,75 @@ class TestNoMatchExceptionForDropdown:
)
assert isinstance(exc, NoAvailableOptionFoundForCustomSelection)
assert exc.observed_options_count == 2
+
+
+class TestSelectFromDropdownByValueNoMatch:
+ @pytest.mark.asyncio
+ async def test_returns_failure_when_no_dropdown_menu_matches(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(handler, "locate_dropdown_menu", AsyncMock(return_value=None))
+
+ result = await handler.select_from_dropdown_by_value(
+ value="Missing",
+ page=MagicMock(),
+ skyvern_element=_FakeAnchorElement(), # type: ignore[arg-type]
+ skyvern_frame=MagicMock(),
+ dom=MagicMock(),
+ incremental_scraped=_FakeValueFallbackScrapePage(), # type: ignore[arg-type]
+ task=_task(), # type: ignore[arg-type]
+ step=MagicMock(),
+ )
+
+ assert isinstance(result, handler.ActionFailure)
+ assert result.exception_type == NoElementMatchedForTargetOption.__name__
+ assert "No value matched" in (result.exception_message or "")
+
+ @pytest.mark.asyncio
+ async def test_returns_failure_when_dropdown_cannot_scroll(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ dropdown_menu = _FakeDropdownMenuElement()
+ skyvern_frame = MagicMock()
+ skyvern_frame.get_element_scrollable = AsyncMock(return_value=False)
+
+ monkeypatch.setattr(handler, "locate_dropdown_menu", AsyncMock(return_value=dropdown_menu))
+ monkeypatch.setattr(handler, "try_to_find_potential_scrollable_element", AsyncMock(return_value=dropdown_menu))
+
+ result = await handler.select_from_dropdown_by_value(
+ value="Missing",
+ page=MagicMock(),
+ skyvern_element=_FakeAnchorElement(), # type: ignore[arg-type]
+ skyvern_frame=skyvern_frame,
+ dom=MagicMock(),
+ incremental_scraped=_FakeValueFallbackScrapePage(), # type: ignore[arg-type]
+ task=_task(), # type: ignore[arg-type]
+ step=MagicMock(),
+ )
+
+ assert isinstance(result, handler.ActionFailure)
+ assert result.exception_type == NoElementMatchedForTargetOption.__name__
+ assert "can't scroll" in (result.exception_message or "")
+
+ @pytest.mark.asyncio
+ async def test_returns_failure_after_scrolling_without_match(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ dropdown_menu = _FakeDropdownMenuElement()
+ skyvern_frame = MagicMock()
+ skyvern_frame.get_element_scrollable = AsyncMock(return_value=True)
+ scroll_down_to_load_all_options = AsyncMock()
+
+ monkeypatch.setattr(handler, "locate_dropdown_menu", AsyncMock(return_value=dropdown_menu))
+ monkeypatch.setattr(handler, "try_to_find_potential_scrollable_element", AsyncMock(return_value=dropdown_menu))
+ monkeypatch.setattr(handler, "scroll_down_to_load_all_options", scroll_down_to_load_all_options)
+
+ result = await handler.select_from_dropdown_by_value(
+ value="Missing",
+ page=MagicMock(),
+ skyvern_element=_FakeAnchorElement(), # type: ignore[arg-type]
+ skyvern_frame=skyvern_frame,
+ dom=MagicMock(),
+ incremental_scraped=_FakeValueFallbackScrapePage(), # type: ignore[arg-type]
+ task=_task(), # type: ignore[arg-type]
+ step=MagicMock(),
+ )
+
+ assert isinstance(result, handler.ActionFailure)
+ assert result.exception_type == NoElementMatchedForTargetOption.__name__
+ assert "after scrolling" in (result.exception_message or "")
+ scroll_down_to_load_all_options.assert_awaited_once()