Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(webeye): preserve no-match signal from custom-select handler (#6762)
LawyZhengd0655589ccfb
diff --git a/skyvern/exceptions.py b/skyvern/exceptions.pyindex f16e75555..1d8117de7 100644--- a/skyvern/exceptions.py+++ b/skyvern/exceptions.py@@ -915,8 +915,31 @@ class NoIncrementalElementFoundForCustomSelection(SkyvernException):class NoAvailableOptionFoundForCustomSelection(SkyvernException):- def __init__(self, reason: str | None) -> None:- super().__init__(f"No available option to select. reason: {reason}.")+ """Raised when the dropdown was populated but no option matched the requested target."""++ def __init__(+ self,+ reason: str | None,+ target_value: str | None = None,+ observed_options: list[str] | None = None,+ ) -> None:+ observed_excerpt = observed_options[:5] if observed_options else []+ observed_count = len(observed_options) if observed_options is not None else 0+ parts = ["No available option to select.", "code=OPTION_NOT_AVAILABLE"]+ if target_value:+ parts.append(f"target_value={target_value!r}")+ if observed_options is not None:+ parts.append(f"observed_options_count={observed_count}")+ if observed_excerpt:+ parts.append(f"observed_options_excerpt={observed_excerpt}")+ if reason:+ parts.append(f"reason={reason!r}")+ super().__init__(" ".join(parts))+ self.code = "OPTION_NOT_AVAILABLE"+ self.target_value = target_value+ self.observed_options_count = observed_count+ self.observed_options_excerpt = observed_excerpt+ self.reason = reasonclass NoElementMatchedForTargetOption(SkyvernException):diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 3fdcc6755..21366a008 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -7,6 +7,7 @@ import shutilimport timeimport urllib.parseimport uuid+from collections import dequefrom datetime import datetime, timedelta, timezonefrom pathlib import Pathfrom typing import Any, Awaitable, Callable, List@@ -4584,6 +4585,62 @@ class CustomSelectPromptOptions(BaseModel):target_value: str | None = None+def _collect_option_texts(elements: list[dict]) -> list[str]:+ """BFS over an element tree, returning option-like text in document order with duplicates removed.++ Native ``<select>`` options live on the element's ``options`` field+ (``[{text, value, optionIndex}, ...]``); the scraper skips their child+ ``<option>`` nodes, so this walker must inspect that field directly.+ """+ queue: deque[dict] = deque(elements)+ seen: set[str] = set()+ out: list[str] = []++ def _record(text: str) -> None:+ if text and text not in seen:+ seen.add(text)+ out.append(text)++ while queue:+ node = queue.popleft()+ if not isinstance(node, dict):+ continue+ attrs = node.get("attributes") or {}+ role = str(attrs.get("role") or "").lower()+ tag = str(node.get("tagName") or "").lower()+ if role == "option" or tag in ("li", "option"):+ _record(str(node.get("text") or "").strip())+ for option in node.get("options") or []:+ if not isinstance(option, dict):+ continue+ # Strip text before falling back to value so whitespace-only text+ # (e.g. " ") is treated as missing rather than recorded as empty.+ option_text = str(option.get("text") or "").strip()+ if not option_text:+ option_text = str(option.get("value") or "").strip()+ _record(option_text)+ for child in node.get("children") or []:+ queue.append(child)+ return out+++def _no_match_exception_for_dropdown(+ *,+ reasoning: str | None,+ target_value: str | None,+ observed_options: list[str],+ transient_fallback_element_id: str | None,+) -> 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(+ reason=reasoning,+ target_value=target_value or None,+ observed_options=observed_options,+ )++def _extract_new_subtrees(elements: list[dict], new_ids: set[str]) -> list[dict]:"""Walk *elements* and return the minimal set of subtrees rooted at new IDs.@@ -4702,11 +4759,18 @@ async def select_from_emerging_elements(response=json_response,)- action_type_str: str = json_response.get("action_type", "") or ""- action_type = ActionType(action_type_str.lower())+ # Check the no-match shape before ``ActionType`` coercion — coercing an empty+ # string raises ValueError and would mask the OPTION_NOT_AVAILABLE signal.+ raw_action_type: str = (json_response.get("action_type") or "").lower()element_id: str | None = json_response.get("id", None)- if not element_id or action_type not in [ActionType.CLICK, ActionType.INPUT_TEXT]:- raise NoAvailableOptionFoundForCustomSelection(reason=json_response.get("reasoning"))+ if not element_id or raw_action_type not in (ActionType.CLICK.value, ActionType.INPUT_TEXT.value):+ raise _no_match_exception_for_dropdown(+ reasoning=json_response.get("reasoning"),+ target_value=options.target_value,+ observed_options=_collect_option_texts(new_element_subtrees),+ transient_fallback_element_id=None,+ )+ action_type = ActionType(raw_action_type)new_ids_set = set(new_interactable_element_ids)if element_id not in new_ids_set:@@ -4840,12 +4904,19 @@ async def select_from_dropdown(response=json_response,)- action_type: str = json_response.get("action_type", "") or ""- action_type = action_type.lower()- single_select_result.action_type = ActionType(action_type)+ # Check the no-match shape before ``ActionType`` coercion — coercing an empty+ # string raises ValueError and would mask the OPTION_NOT_AVAILABLE signal.+ raw_action_type: str = (json_response.get("action_type") or "").lower()element_id: str | None = json_response.get("id", None)- if not element_id or action_type not in [ActionType.CLICK, ActionType.INPUT_TEXT]:- raise NoAvailableOptionFoundForCustomSelection(reason=json_response.get("reasoning"))+ if not element_id or raw_action_type not in (ActionType.CLICK.value, ActionType.INPUT_TEXT.value):+ raise _no_match_exception_for_dropdown(+ reasoning=json_response.get("reasoning"),+ target_value=target_value,+ observed_options=_collect_option_texts(trimmed_element_tree),+ transient_fallback_element_id=skyvern_element.get_id(),+ )+ single_select_result.action_type = ActionType(raw_action_type)+ action_type = single_select_result.action_typeif not force_select and target_value:if not json_response.get("relevant", False):diff --git a/tests/unit/test_no_available_option.py b/tests/unit/test_no_available_option.pynew file mode 100644index 000000000..d391a2606--- /dev/null+++ b/tests/unit/test_no_available_option.py@@ -0,0 +1,277 @@+"""Unit tests for the custom-select no-match path."""++from __future__ import annotations++import pytest++from skyvern.exceptions import (+ NoAvailableOptionFoundForCustomSelection,+ NoIncrementalElementFoundForCustomSelection,+)+from skyvern.webeye.actions.handler import (+ _collect_option_texts,+ _no_match_exception_for_dropdown,+)+++class TestCollectOptionTexts:+ def test_extracts_li_option_texts(self) -> None:+ tree = [+ {+ "tagName": "ul",+ "attributes": {"role": "listbox"},+ "children": [+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Alpha"},+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Bravo"},+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Charlie"},+ ],+ }+ ]+ assert _collect_option_texts(tree) == ["Alpha", "Bravo", "Charlie"]++ def test_extracts_native_option_elements(self) -> None:+ tree = [+ {+ "tagName": "select",+ "children": [+ {"tagName": "option", "text": "First"},+ {"tagName": "option", "text": "Second"},+ ],+ }+ ]+ assert _collect_option_texts(tree) == ["First", "Second"]++ def test_ignores_non_option_nodes(self) -> None:+ tree = [+ {"tagName": "div", "text": "header copy"},+ {"tagName": "button", "text": "Submit"},+ {"tagName": "span", "text": "label"},+ ]+ assert _collect_option_texts(tree) == []++ def test_returns_empty_for_empty_tree(self) -> None:+ assert _collect_option_texts([]) == []++ def test_handles_missing_optional_fields(self) -> None:+ tree = [+ {"tagName": "li"},+ {"tagName": "li", "attributes": {"role": "option"}, "text": ""},+ {"tagName": "li", "attributes": {"role": "option"}, "text": " "},+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Real"},+ ]+ assert _collect_option_texts(tree) == ["Real"]++ def test_dedupes_repeated_option_text(self) -> None:+ tree = [+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Alpha"},+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Bravo"},+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Alpha"},+ ]+ assert _collect_option_texts(tree) == ["Alpha", "Bravo"]++ def test_walks_nested_children(self) -> None:+ tree = [+ {+ "tagName": "div",+ "children": [+ {+ "tagName": "ul",+ "attributes": {"role": "listbox"},+ "children": [+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Inner"},+ ],+ }+ ],+ }+ ]+ assert _collect_option_texts(tree) == ["Inner"]++ def test_extracts_div_role_option(self) -> None:+ tree = [+ {+ "tagName": "div",+ "attributes": {"role": "listbox"},+ "children": [+ {"tagName": "div", "attributes": {"role": "option"}, "text": "Alpha"},+ {"tagName": "div", "attributes": {"role": "option"}, "text": "Bravo"},+ ],+ }+ ]+ assert _collect_option_texts(tree) == ["Alpha", "Bravo"]++ def test_extracts_native_select_from_options_field(self) -> None:+ # Scraper stores native <select> options on the element itself and+ # skips child <option> nodes.+ tree = [+ {+ "tagName": "select",+ "options": [+ {"optionIndex": 0, "text": "January", "value": "1"},+ {"optionIndex": 1, "text": "February", "value": "2"},+ {"optionIndex": 2, "text": "March", "value": "3"},+ ],+ }+ ]+ assert _collect_option_texts(tree) == ["January", "February", "March"]++ def test_falls_back_to_value_when_options_text_is_empty(self) -> None:+ tree = [+ {+ "tagName": "select",+ "options": [+ {"optionIndex": 0, "text": "", "value": "Q1"},+ {"optionIndex": 1, "text": "Two", "value": "2"},+ ],+ }+ ]+ assert _collect_option_texts(tree) == ["Q1", "Two"]++ def test_falls_back_to_value_when_options_text_is_whitespace_only(self) -> None:+ tree = [+ {+ "tagName": "select",+ "options": [+ {"optionIndex": 0, "text": " ", "value": "Q1"},+ {"optionIndex": 1, "text": "\t\n", "value": "Q2"},+ {"optionIndex": 2, "text": "Real", "value": "x"},+ ],+ }+ ]+ assert _collect_option_texts(tree) == ["Q1", "Q2", "Real"]++ def test_dedupes_across_li_and_options_field(self) -> None:+ tree = [+ {+ "tagName": "select",+ "options": [{"optionIndex": 0, "text": "Alpha", "value": "a"}],+ },+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Alpha"},+ {"tagName": "li", "attributes": {"role": "option"}, "text": "Bravo"},+ ]+ assert _collect_option_texts(tree) == ["Alpha", "Bravo"]+++class TestNoAvailableOptionFoundForCustomSelection:+ def test_message_includes_code_target_count_excerpt_and_reason(self) -> None:+ exc = NoAvailableOptionFoundForCustomSelection(+ reason="not present in the list",+ target_value="Target Value",+ observed_options=["Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot"],+ )+ msg = str(exc)+ assert "code=OPTION_NOT_AVAILABLE" in msg+ assert "target_value='Target Value'" in msg+ assert "observed_options_count=6" in msg+ assert "['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo']" in msg+ assert "Foxtrot" not in msg # excerpt is capped at 5+ assert "not present in the list" in msg++ def test_attributes_are_accessible_for_downstream_consumers(self) -> None:+ exc = NoAvailableOptionFoundForCustomSelection(+ reason="not in dropdown",+ target_value="Target",+ observed_options=["Alpha", "Bravo"],+ )+ assert exc.code == "OPTION_NOT_AVAILABLE"+ assert exc.target_value == "Target"+ assert exc.observed_options_count == 2+ assert exc.observed_options_excerpt == ["Alpha", "Bravo"]+ assert exc.reason == "not in dropdown"++ def test_omits_optional_fields_when_not_supplied(self) -> None:+ exc = NoAvailableOptionFoundForCustomSelection(reason=None)+ msg = str(exc)+ assert "code=OPTION_NOT_AVAILABLE" in msg+ assert "target_value" not in msg+ assert "observed_options_count" not in msg+ assert "observed_options_excerpt" not in msg+ assert exc.target_value is None+ assert exc.observed_options_count == 0+ assert exc.observed_options_excerpt == []++ def test_no_value_error_when_constructed_from_empty_no_match_payload(self) -> None:+ # Regression: previously ActionType("") fired on this payload before the+ # OPTION_NOT_AVAILABLE branch could run.+ json_response = {"action_type": "", "id": "", "reasoning": "not present", "relevant": False}+ try:+ raise NoAvailableOptionFoundForCustomSelection(+ reason=json_response["reasoning"],+ target_value="Anything",+ observed_options=["Alpha"],+ )+ except ValueError:+ pytest.fail("ValueError leaked from no-match exception construction")+ except NoAvailableOptionFoundForCustomSelection as exc:+ assert exc.code == "OPTION_NOT_AVAILABLE"+++class TestNoMatchExceptionForDropdown:+ def test_returns_transient_when_no_options_and_fallback_id_given(self) -> None:+ exc = _no_match_exception_for_dropdown(+ reasoning="dropdown empty",+ target_value="Target",+ observed_options=[],+ transient_fallback_element_id="element-123",+ )+ assert isinstance(exc, NoIncrementalElementFoundForCustomSelection)+ assert "element-123" in str(exc)++ def test_returns_permanent_when_options_observed(self) -> None:+ exc = _no_match_exception_for_dropdown(+ reasoning="target not in list",+ target_value="Target",+ observed_options=["Alpha", "Bravo"],+ transient_fallback_element_id="element-123",+ )+ assert isinstance(exc, NoAvailableOptionFoundForCustomSelection)+ assert exc.target_value == "Target"+ assert exc.observed_options_count == 2+ assert exc.observed_options_excerpt == ["Alpha", "Bravo"]+ assert exc.reason == "target not in list"++ def test_returns_permanent_when_no_options_but_no_fallback_id(self) -> None:+ # The emerging-element path passes None: an upstream guard handles the+ # zero-options case there, so this branch must surface as permanent.+ exc = _no_match_exception_for_dropdown(+ reasoning="target not in list",+ target_value="Target",+ observed_options=[],+ transient_fallback_element_id=None,+ )+ assert isinstance(exc, NoAvailableOptionFoundForCustomSelection)+ assert exc.observed_options_count == 0+ assert exc.observed_options_excerpt == []++ def test_normalizes_empty_target_value_to_none(self) -> None:+ exc = _no_match_exception_for_dropdown(+ reasoning=None,+ target_value="",+ observed_options=["Alpha"],+ transient_fallback_element_id=None,+ )+ assert isinstance(exc, NoAvailableOptionFoundForCustomSelection)+ assert exc.target_value is None++ 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+ # exception. Walker first, then helper, end-to-end on the F-guard.+ tree = [+ {+ "tagName": "select",+ "options": [+ {"optionIndex": 0, "text": "January", "value": "1"},+ {"optionIndex": 1, "text": "February", "value": "2"},+ ],+ }+ ]+ observed = _collect_option_texts(tree)+ assert observed == ["January", "February"]+ exc = _no_match_exception_for_dropdown(+ reasoning="target not in list",+ target_value="December",+ observed_options=observed,+ transient_fallback_element_id="select-element-id",+ )+ assert isinstance(exc, NoAvailableOptionFoundForCustomSelection)+ assert exc.observed_options_count == 2