Skip to content

Automate browser based workflows with AI

Handle optional custom-selection misses gracefully; keep required misses typed (SKY-12678, SKY-12631, SKY-12514, SKY-12700) (#7650)

Aaron Perez61d3399b7624
diff --git a/skyvern/exceptions.py b/skyvern/exceptions.py
index 1f98176a4..1446f7af5 100644
--- a/skyvern/exceptions.py
+++ b/skyvern/exceptions.py
@@ -1025,6 +1025,9 @@ class NoAvailableOptionFoundForCustomSelection(SkyvernException):
self.observed_options_count = observed_count
self.observed_options_excerpt = observed_excerpt
self.reason = reason
+ # Set True when an earlier level of a cascading select already committed a click before this
+ # miss, so the widget is partially mutated and the miss must not be reported as a clean skip.
+ self.widget_mutated = False
class NoElementMatchedForTargetOption(SkyvernException):
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index 22aab70bc..c423cffb6 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -4823,12 +4823,17 @@ async def handle_select_option_action(
is_open = False
suggested_value: str | None = None
results: list[ActionResult] = []
+ input_or_select_context: InputOrSelectContext | None = None
try:
await incremental_scraped.start_listen_dom_increment(await skyvern_element.get_element_handler())
await skyvern_element.scroll_into_view()
await skyvern_element.click(page=page, dom=dom, timeout=timeout)
+ # The click opens the widget: mark it open now (not only on the incremental path below) so the
+ # finally cleanup dismisses it on every exit — including an emerging-path optional miss that
+ # returns ActionAbort before reaching the incremental branch.
+ is_open = True
# wait for options to load
await skyvern_frame.safe_wait_for_animation_end(before_wait_sec=0.5, caller="select_option.open")
@@ -4879,7 +4884,6 @@ async def handle_select_option_action(
)
return results
- is_open = True
# TODO: support sequetially select from dropdown by value, just support single select now
result = await sequentially_select_from_dropdown(
action=action,
@@ -4905,6 +4909,28 @@ async def handle_select_option_action(
return results
suggested_value = result.value
+ except NoAvailableOptionFoundForCustomSelection as e:
+ # Skip only a field known to be optional whose widget was left untouched. Requiredness is
+ # LLM-populated and may be None (undetermined) — fail closed there. Also fail closed when an
+ # earlier cascade level already committed a click (e.widget_mutated): a partially-selected
+ # widget must surface the typed OPTION_NOT_AVAILABLE failure/retry, not a clean skip.
+ if (
+ input_or_select_context is not None
+ and input_or_select_context.is_required is False
+ and not e.widget_mutated
+ ):
+ LOG.info(
+ "Optional custom-select found no matching option; recording an optional miss and skipping the step",
+ target_value=action.option.label or action.option.value,
+ )
+ results.append(ActionAbort())
+ return results
+ LOG.warning(
+ "Custom-select found no matching option for a required, unknown-requiredness, or partially-mutated field",
+ exc_info=True,
+ )
+ results.append(ActionFailure(exception=e))
+ return results
except SkyvernException as e:
# Expected selection outcomes on non-standard dropdowns (no matching option,
# no incremental elements); recorded as ActionFailure like any other miss.
@@ -6844,22 +6870,30 @@ async def sequentially_select_from_dropdown(
check_filter_funcs: list[CheckFilterOutElementIDFunc] = [check_existed_but_not_option_element_in_dom_factory(dom)]
for i in range(max_depth):
- single_select_result = await select_from_dropdown(
- context=input_or_select_context,
- page=page,
- skyvern_element=skyvern_element,
- skyvern_frame=skyvern_frame,
- incremental_scraped=incremental_scraped,
- check_filter_funcs=check_filter_funcs,
- step=step,
- task=task,
- dropdown_menu_element=dropdown_menu_element,
- select_history=select_history,
- force_select=force_select,
- target_value=target_value,
- entry_action_type=entry_action_type,
- selection_group_id=selection_group_id,
- )
+ try:
+ single_select_result = await select_from_dropdown(
+ context=input_or_select_context,
+ page=page,
+ skyvern_element=skyvern_element,
+ skyvern_frame=skyvern_frame,
+ incremental_scraped=incremental_scraped,
+ check_filter_funcs=check_filter_funcs,
+ step=step,
+ task=task,
+ dropdown_menu_element=dropdown_menu_element,
+ select_history=select_history,
+ force_select=force_select,
+ target_value=target_value,
+ entry_action_type=entry_action_type,
+ selection_group_id=selection_group_id,
+ )
+ except NoAvailableOptionFoundForCustomSelection as e:
+ # The loop only advances past a level whose click succeeded (is_done() gates on
+ # ActionSuccess), so any prior history here means an earlier cascade level already
+ # mutated the widget — mark it so the caller can't report this miss as a clean skip.
+ if any(isinstance(prior.action_result, ActionSuccess) for prior in select_history):
+ e.widget_mutated = True
+ raise
assert single_select_result is not None
select_history.append(single_select_result)
values.append(single_select_result.value)
@@ -7505,6 +7539,7 @@ async def _select_deterministic_custom_option(
entry_action_type: str = "select_option",
selection_group_id: str | None = None,
select_depth: int = 0,
+ on_click_attempted: Callable[[], None] | None = None,
) -> tuple[ActionResult, str | None] | None:
started_at = time.monotonic()
selection_group_id = selection_group_id or str(uuid.uuid4())
@@ -7646,6 +7681,8 @@ async def _select_deterministic_custom_option(
await selected_element.scroll_into_view()
click_attempted = True
+ if on_click_attempted is not None:
+ on_click_attempted()
await selected_element.click(page=page)
verified = await _verify_custom_select_option_with_settle(
matched_element=selected_element,
@@ -7740,15 +7777,18 @@ def _no_match_exception_for_dropdown(
target_value: str | None,
observed_options: list[str],
transient_fallback_element_id: str | None,
+ widget_mutated: bool = False,
) -> Exception:
"""Return the right no-match exception: transient when the dropdown opened with zero options, permanent otherwise."""
if not observed_options and transient_fallback_element_id is not None:
return NoIncrementalElementFoundForCustomSelection(element_id=transient_fallback_element_id)
- return NoAvailableOptionFoundForCustomSelection(
+ exc = NoAvailableOptionFoundForCustomSelection(
reason=reasoning,
target_value=target_value or None,
observed_options=observed_options,
)
+ exc.widget_mutated = widget_mutated
+ return exc
def _extract_new_subtrees(elements: list[dict], new_ids: set[str]) -> list[dict]:
@@ -7853,6 +7893,12 @@ async def select_from_emerging_elements(
async def get_readback_scope_element() -> SkyvernElement | None:
return await dom_after_open.get_skyvern_element_by_id(current_element_id)
+ widget_mutated = False
+
+ def _mark_widget_mutated() -> None:
+ nonlocal widget_mutated
+ widget_mutated = True
+
deterministic_result = await _select_deterministic_custom_option(
target_value=options.target_value,
get_option_candidates=lambda: _custom_select_candidates_from_elements(shadow_candidate_elements),
@@ -7865,6 +7911,7 @@ async def select_from_emerging_elements(
entry_action_type=entry_action_type,
selection_group_id=selection_group_id,
select_depth=0,
+ on_click_attempted=_mark_widget_mutated,
)
if deterministic_result is not None:
action_result, _matched_label = deterministic_result
@@ -7914,6 +7961,7 @@ async def select_from_emerging_elements(
target_value=options.target_value,
observed_options=_collect_option_texts(new_element_subtrees),
transient_fallback_element_id=None,
+ widget_mutated=widget_mutated,
)
action_type = ActionType(raw_action_type)
@@ -8023,6 +8071,12 @@ async def select_from_dropdown(
incremental_scraped.set_element_tree_trimmed(trimmed_element_tree)
html = incremental_scraped.build_element_tree(html_need_skyvern_attrs=True)
+ widget_mutated = False
+
+ def _mark_widget_mutated() -> None:
+ nonlocal widget_mutated
+ widget_mutated = True
+
deterministic_result = await _select_deterministic_custom_option(
target_value=target_value,
get_option_candidates=lambda: _custom_select_candidates_from_elements(trimmed_element_tree),
@@ -8035,6 +8089,7 @@ async def select_from_dropdown(
entry_action_type=entry_action_type,
selection_group_id=selection_group_id or str(uuid.uuid4()),
select_depth=len(select_history),
+ on_click_attempted=_mark_widget_mutated,
)
if deterministic_result is not None:
action_result, matched_label = deterministic_result
@@ -8095,6 +8150,7 @@ async def select_from_dropdown(
target_value=target_value,
observed_options=_collect_option_texts(trimmed_element_tree),
transient_fallback_element_id=skyvern_element.get_id(),
+ widget_mutated=widget_mutated,
)
single_select_result.action_type = ActionType(raw_action_type)
action_type = single_select_result.action_type
diff --git a/tests/unit/test_no_available_option.py b/tests/unit/test_no_available_option.py
index c485db654..c6d5af1fe 100644
--- a/tests/unit/test_no_available_option.py
+++ b/tests/unit/test_no_available_option.py
@@ -14,8 +14,10 @@ from skyvern.exceptions import (
NoIncrementalElementFoundForCustomSelection,
)
from skyvern.forge.agent_functions import AgentFunction
+from skyvern.forge.sdk.core import skyvern_context
+from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.webeye.actions import handler
-from skyvern.webeye.actions.actions import InputOrSelectContext, SelectOption, SelectOptionAction
+from skyvern.webeye.actions.actions import ActionType, InputOrSelectContext, SelectOption, SelectOptionAction
from skyvern.webeye.actions.handler import (
_collect_option_texts,
_custom_select_candidates_from_elements,
@@ -1605,6 +1607,26 @@ class TestNoMatchExceptionForDropdown:
assert isinstance(exc, NoAvailableOptionFoundForCustomSelection)
assert exc.target_value is None
+ def test_widget_mutated_defaults_false_and_propagates_when_set(self) -> None:
+ default = _no_match_exception_for_dropdown(
+ reasoning=None,
+ target_value="Target",
+ observed_options=["Alpha"],
+ transient_fallback_element_id=None,
+ )
+ assert isinstance(default, NoAvailableOptionFoundForCustomSelection)
+ assert default.widget_mutated is False
+
+ mutated = _no_match_exception_for_dropdown(
+ reasoning=None,
+ target_value="Target",
+ observed_options=["Alpha"],
+ transient_fallback_element_id=None,
+ widget_mutated=True,
+ )
+ assert isinstance(mutated, NoAvailableOptionFoundForCustomSelection)
+ assert mutated.widget_mutated is True
+
def test_native_select_populated_routes_to_permanent_not_transient(self) -> None:
# Regression: a native <select> populated via element["options"]
# must NOT be misread as zero-options and routed to the transient
@@ -1700,3 +1722,275 @@ class TestSelectFromDropdownByValueNoMatch:
assert result.exception_type == NoElementMatchedForTargetOption.__name__
assert "after scrolling" in (result.exception_message or "")
scroll_down_to_load_all_options.assert_awaited_once()
+
+
+class TestCustomSelectMissRespectsOptionality:
+ @staticmethod
+ def _patch_open_dropdown_then_miss(
+ monkeypatch: pytest.MonkeyPatch, *, is_required: bool | None, widget_mutated: bool = False
+ ) -> AsyncMock:
+ """Drive handle_select_option_action to the no-match raise, returning the value-fallback mock."""
+ anchor_element = _FakeAnchorElement()
+ fake_frame = MagicMock()
+ fake_frame.safe_wait_for_animation_end = AsyncMock()
+ fake_incremental = _FakeIncrementalScrapePage([[{"id": "opened-option"}]])
+
+ class FakeDomUtil:
+ def __init__(self, *_args: object, **_kwargs: object) -> None:
+ pass
+
+ async def get_skyvern_element_by_id(self, _element_id: str) -> _FakeAnchorElement:
+ return anchor_element
+
+ miss = NoAvailableOptionFoundForCustomSelection(
+ reason="target not in list",
+ target_value="Choice",
+ observed_options=["Alpha", "Bravo"],
+ )
+ miss.widget_mutated = widget_mutated
+ value_fallback = AsyncMock(return_value=handler.ActionSuccess())
+ monkeypatch.setattr(handler, "DomUtil", FakeDomUtil)
+ monkeypatch.setattr(handler.SkyvernFrame, "create_instance", AsyncMock(return_value=fake_frame))
+ monkeypatch.setattr(handler, "IncrementalScrapePage", MagicMock(return_value=fake_incremental))
+ monkeypatch.setattr(
+ handler,
+ "_get_input_or_select_context",
+ AsyncMock(return_value=InputOrSelectContext(field="Field", is_required=is_required)),
+ )
+ monkeypatch.setattr(handler, "sequentially_select_from_dropdown", AsyncMock(side_effect=miss))
+ monkeypatch.setattr(handler, "select_from_dropdown_by_value", value_fallback)
+ return value_fallback
+
+ async def _run(self) -> list[handler.ActionResult]:
+ return await handler.handle_select_option_action(
+ action=_select_action(),
+ page=MagicMock(),
+ scraped_page=SimpleNamespace(
+ id_to_element_dict={"field-control": {"id": "field-control"}},
+ id_to_css_dict={},
+ ),
+ task=_task(), # type: ignore[arg-type]
+ step=MagicMock(),
+ )
+
+ @pytest.mark.asyncio
+ async def test_optional_miss_records_abort_and_skips_value_fallback(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ value_fallback = self._patch_open_dropdown_then_miss(monkeypatch, is_required=False)
+
+ results = await self._run()
+
+ assert len(results) == 1
+ assert isinstance(results[0], handler.ActionAbort)
+ assert results[0].success is True
+ value_fallback.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_required_miss_records_typed_option_not_available_failure(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ value_fallback = self._patch_open_dropdown_then_miss(monkeypatch, is_required=True)
+
+ results = await self._run()
+
+ assert len(results) == 1
+ assert isinstance(results[0], handler.ActionFailure)
+ assert results[0].exception_type == NoAvailableOptionFoundForCustomSelection.__name__
+ assert "OPTION_NOT_AVAILABLE" in (results[0].exception_message or "")
+ value_fallback.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_unknown_requiredness_miss_fails_closed_not_skipped(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ # is_required is LLM-populated and may be None; an undetermined field must fail closed
+ # (typed failure) rather than silently skip a possibly-required selection.
+ value_fallback = self._patch_open_dropdown_then_miss(monkeypatch, is_required=None)
+
+ results = await self._run()
+
+ assert len(results) == 1
+ assert isinstance(results[0], handler.ActionFailure)
+ assert results[0].exception_type == NoAvailableOptionFoundForCustomSelection.__name__
+ assert "OPTION_NOT_AVAILABLE" in (results[0].exception_message or "")
+ value_fallback.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_optional_miss_after_prior_cascade_click_fails_closed_not_skipped(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ # A cascading optional select that clicked an earlier level before a deeper miss left the
+ # widget partially mutated; it must fail closed rather than report a clean skip.
+ value_fallback = self._patch_open_dropdown_then_miss(monkeypatch, is_required=False, widget_mutated=True)
+
+ results = await self._run()
+
+ assert len(results) == 1
+ assert isinstance(results[0], handler.ActionFailure)
+ assert results[0].exception_type == NoAvailableOptionFoundForCustomSelection.__name__
+ value_fallback.assert_not_awaited()
+
+ @pytest.mark.asyncio
+ async def test_emerging_path_optional_miss_closes_open_dropdown(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ # The emerging (re-scrape) path raises before the old is_open assignment, so its optional
+ # miss must still dismiss the dropdown the anchor click opened (coordinate click + Escape).
+ anchor_element = _FakeAnchorElement()
+ anchor_element.is_visible = AsyncMock(return_value=True)
+ fake_frame = MagicMock()
+ fake_frame.safe_wait_for_animation_end = AsyncMock()
+ # Empty incremental tree routes into select_from_emerging_elements.
+ fake_incremental = _FakeIncrementalScrapePage([[]])
+
+ class FakeDomUtil:
+ def __init__(self, *_args: object, **_kwargs: object) -> None:
+ pass
+
+ async def get_skyvern_element_by_id(self, _element_id: str) -> _FakeAnchorElement:
+ return anchor_element
+
+ value_fallback = AsyncMock(return_value=handler.ActionSuccess())
+ monkeypatch.setattr(handler, "DomUtil", FakeDomUtil)
+ monkeypatch.setattr(handler.SkyvernFrame, "create_instance", AsyncMock(return_value=fake_frame))
+ monkeypatch.setattr(handler, "IncrementalScrapePage", MagicMock(return_value=fake_incremental))
+ monkeypatch.setattr(
+ handler,
+ "_get_input_or_select_context",
+ AsyncMock(return_value=InputOrSelectContext(field="Field", is_required=False)),
+ )
+ monkeypatch.setattr(
+ handler,
+ "select_from_emerging_elements",
+ AsyncMock(
+ side_effect=NoAvailableOptionFoundForCustomSelection(
+ reason="target not in list", target_value="Choice", observed_options=["Alpha"]
+ )
+ ),
+ )
+ monkeypatch.setattr(handler, "select_from_dropdown_by_value", value_fallback)
+
+ results = await self._run()
+
+ assert len(results) == 1
+ assert isinstance(results[0], handler.ActionAbort)
+ anchor_element.coordinate_click.assert_awaited()
+ anchor_element.press_key.assert_any_await("Escape")
+ value_fallback.assert_not_awaited()
+
+
+class TestSequentialSelectMarksWidgetMutation:
+ @staticmethod
+ def _level_result(*, action_result: object, action_type: object, dropdown_menu: object) -> SimpleNamespace:
+ return SimpleNamespace(
+ action_result=action_result,
+ action_type=action_type,
+ dropdown_menu=dropdown_menu,
+ value="Level0",
+ is_done=AsyncMock(return_value=False),
+ )
+
+ @pytest.mark.asyncio
+ async def test_marks_mutation_when_earlier_level_clicked_before_deeper_miss(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ skyvern_frame = MagicMock()
+ skyvern_frame.safe_wait_for_animation_end = AsyncMock()
+ incremental_scraped = MagicMock()
+ incremental_scraped.get_incremental_element_tree = AsyncMock(return_value=[{"id": "next-level"}])
+
+ # Level 0 commits a click (ActionSuccess) and stays open as an INPUT_TEXT level so the loop
+ # advances straight to level 1 without the confirm-finish screenshot/LLM; level 1 misses.
+ level0 = self._level_result(
+ action_result=handler.ActionSuccess(),
+ action_type=ActionType.INPUT_TEXT,
+ dropdown_menu=object(),
+ )
+ deeper_miss = NoAvailableOptionFoundForCustomSelection(
+ reason="target not in list", target_value="Choice", observed_options=["Alpha"]
+ )
+ monkeypatch.setattr(handler, "select_from_dropdown", AsyncMock(side_effect=[level0, deeper_miss]))
+
+ with pytest.raises(NoAvailableOptionFoundForCustomSelection) as excinfo:
+ await handler.sequentially_select_from_dropdown(
+ action=_select_action(),
+ input_or_select_context=InputOrSelectContext(field="Field", is_required=False),
+ page=MagicMock(),
+ dom=MagicMock(),
+ skyvern_element=_FakeAnchorElement(), # type: ignore[arg-type]
+ skyvern_frame=skyvern_frame,
+ incremental_scraped=incremental_scraped,
+ step=MagicMock(),
+ task=_task(), # type: ignore[arg-type]
+ force_select=True,
+ target_value="Choice",
+ )
+
+ assert excinfo.value.widget_mutated is True
+
+ @pytest.mark.asyncio
+ async def test_first_level_miss_leaves_widget_unmutated(self, monkeypatch: pytest.MonkeyPatch) -> None:
+ skyvern_frame = MagicMock()
+ skyvern_frame.safe_wait_for_animation_end = AsyncMock()
+ first_miss = NoAvailableOptionFoundForCustomSelection(
+ reason="target not in list", target_value="Choice", observed_options=["Alpha"]
+ )
+ monkeypatch.setattr(handler, "select_from_dropdown", AsyncMock(side_effect=first_miss))
+
+ with pytest.raises(NoAvailableOptionFoundForCustomSelection) as excinfo:
+ await handler.sequentially_select_from_dropdown(
+ action=_select_action(),
+ input_or_select_context=InputOrSelectContext(field="Field", is_required=False),
+ page=MagicMock(),
+ dom=MagicMock(),
+ skyvern_element=_FakeAnchorElement(), # type: ignore[arg-type]
+ skyvern_frame=skyvern_frame,
+ incremental_scraped=MagicMock(),
+ step=MagicMock(),
+ task=_task(), # type: ignore[arg-type]
+ force_select=True,
+ target_value="Choice",
+ )
+
+ assert excinfo.value.widget_mutated is False
+
+
+class TestSameLevelClickMarksWidgetMutation:
+ @pytest.mark.asyncio
+ async def test_select_from_dropdown_marks_mutation_when_deterministic_clicked_then_missed(
+ self, monkeypatch: pytest.MonkeyPatch
+ ) -> None:
+ # The deterministic path can click the current candidate and still return None (route to
+ # LLM), so a same-level click followed by an LLM no-match must mark the widget mutated even
+ # though nothing is recorded in select_history.
+ async def fake_deterministic(*, on_click_attempted: object = None, **_kwargs: object) -> None:
+ if on_click_attempted is not None:
+ on_click_attempted() # type: ignore[operator]
+ return None
+
+ monkeypatch.setattr(handler, "_select_deterministic_custom_option", fake_deterministic)
+ monkeypatch.setattr(handler.prompt_engine, "load_prompt", MagicMock(return_value="prompt"))
+ monkeypatch.setattr(
+ handler.app,
+ "CUSTOM_SELECT_AGENT_LLM_API_HANDLER",
+ AsyncMock(return_value={"action_type": "", "id": "", "reasoning": "not present"}),
+ )
+ monkeypatch.setattr(handler, "locate_dropdown_menu", AsyncMock(return_value=None))
+ fake_incremental = _FakeIncrementalScrapePage(
+ [[{"tagName": "li", "attributes": {"role": "option"}, "text": "Alpha"}]]
+ )
+
+ skyvern_context.set(SkyvernContext(task_id="tsk-test"))
+ try:
+ with pytest.raises(NoAvailableOptionFoundForCustomSelection) as excinfo:
+ await handler.select_from_dropdown(
+ context=InputOrSelectContext(field="Field", is_required=False),
+ page=MagicMock(),
+ skyvern_element=_FakeAnchorElement(), # type: ignore[arg-type]
+ skyvern_frame=MagicMock(),
+ incremental_scraped=fake_incremental, # type: ignore[arg-type]
+ check_filter_funcs=[],
+ step=MagicMock(),
+ task=_task(), # type: ignore[arg-type]
+ force_select=True,
+ target_value="Choice",
+ )
+ finally:
+ skyvern_context.reset()
+
+ assert excinfo.value.widget_mutated is True