Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-12399): surface a diagnostic when a submit click does not advance the form (#7520)
Aaron Perez29e4fb99ecbd
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex a5b67da27..7c3fe9ef1 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -173,6 +173,7 @@ UPLOAD_PENDING_FOLLOWUP_MESSAGE = "Upload is not complete yet. Continue the uploFIX_TEL_INPUT_DIGIT_DROP_FLAG = "FIX_TEL_INPUT_DIGIT_DROP"VERIFY_EMERGING_SELECT_PICK_FLAG = "VERIFY_EMERGING_SELECT_PICK"FIX_MASKED_INPUT_READBACK_FLAG = "FIX_MASKED_INPUT_READBACK"+SURFACE_BLOCKED_FORM_ADVANCE_FLAG = "SURFACE_BLOCKED_FORM_ADVANCE"COLLAPSE_SELECT_FANOUT_FLAG = "COLLAPSE_SELECT_FANOUT"COLLAPSE_CUSTOM_SELECT_FANOUT_FLAG = "COLLAPSE_CUSTOM_SELECT_FANOUT"COLLAPSE_AUTOCOMPLETE_FANOUT_FLAG = "COLLAPSE_AUTOCOMPLETE_FANOUT"@@ -1211,6 +1212,10 @@ async def _is_masked_input_readback_fix_enabled(task: Task) -> bool:return await _is_org_flag_enabled(task, FIX_MASKED_INPUT_READBACK_FLAG, "masked-input-readback")+async def _is_surface_blocked_form_advance_enabled(task: Task) -> bool:+ return await _is_org_flag_enabled(task, SURFACE_BLOCKED_FORM_ADVANCE_FLAG, "surface-blocked-form-advance")++async def _resolve_collapse_xp_assignment(experimentation_provider: BaseExperimentationProvider,task: Task,@@ -2670,6 +2675,91 @@ async def _retarget_disabled_element_for_click(return child_element+_SUBMIT_LIKE_TEXT = re.compile(+ r"\b(next|submit|continue|proceed|confirm|finish|save|apply|update|complete|register|sign\s?up|log\s?in)\b",+ re.IGNORECASE,+)++_BLOCKED_FORM_ADVANCE_MESSAGE = (+ "Clicked '{label}' but the page did not advance and no new elements appeared. The form is likely "+ "blocked by a validation error — inspect the visible fields for invalid or required inputs and any "+ "error messages, resolve them, and only then click to advance again instead of repeating this click."+)+++def _submit_like_label(element_dict: dict) -> str:+ attributes = element_dict.get("attributes") or {}+ for candidate in (+ element_dict.get("text"),+ attributes.get("value"),+ attributes.get("aria-label"),+ attributes.get("title"),+ ):+ text = str(candidate or "").strip()+ if text:+ return text+ return "the button"+++def _click_is_submit_like(element_dict: dict) -> bool:+ attributes = element_dict.get("attributes") or {}+ tag = str(element_dict.get("tagName") or "").lower()+ input_type = str(attributes.get("type") or "").lower()+ if tag == "input" and input_type in {"submit", "image"}:+ return True+ role = str(attributes.get("role") or attributes.get("aria-role") or "").lower()+ # A bare <a> is a navigation link (a real one changes the URL and is excluded by the+ # caller's URL-change check); only an <a role="button"> counts as a submit control.+ is_button_like = tag == "button" or role == "button" or (tag == "input" and input_type == "button")+ if not is_button_like:+ return False+ label = " ".join(+ str(part)+ for part in (+ element_dict.get("text"),+ attributes.get("value"),+ attributes.get("aria-label"),+ attributes.get("title"),+ )+ if part+ )+ return bool(_SUBMIT_LIKE_TEXT.search(label))+++async def _maybe_flag_blocked_form_advance(+ *,+ results: list[ActionResult],+ skyvern_element: SkyvernElement,+ page: Page,+ original_url: str,+ incremental_scraped: IncrementalScrapePage,+ task: Task,+) -> None:+ """Surface a diagnostic when a submit-like click neither navigated nor revealed new elements.++ The signal flows to the next step's planner through ActionResult.followup_message (see+ get_action_history), directing the agent to resolve the blocking validation instead of+ re-clicking a button that is not advancing the form.+ """+ if not results or not isinstance(results[-1], ActionSuccess):+ return+ element_dict = skyvern_element.get_element_dict()+ if not _click_is_submit_like(element_dict):+ return+ if page.url != original_url:+ return+ if not await _is_surface_blocked_form_advance_enabled(task):+ return+ if await incremental_scraped.get_incremental_elements_num() > 0:+ return+ results[-1].needs_followup = True+ results[-1].followup_message = _BLOCKED_FORM_ADVANCE_MESSAGE.format(label=_submit_like_label(element_dict))+ LOG.info(+ "Surfacing blocked-form-advance diagnostic after a non-advancing submit-like click",+ element_id=skyvern_element.get_id(),+ )++@traced(name="skyvern.agent.action.click")async def handle_click_action(action: actions.ClickAction,@@ -2828,6 +2918,17 @@ async def handle_click_action(results.append(sequential_click_result)return results+ # No navigation and no dropdown surfaced: flag a non-advancing submit-like click so the+ # next step's planner resolves the blocking validation instead of re-clicking.+ await _maybe_flag_blocked_form_advance(+ results=results,+ skyvern_element=skyvern_element,+ page=page,+ original_url=original_url,+ incremental_scraped=incremental_scraped,+ task=task,+ )+except Exception:LOG.warning("Failed to do sequential logic for the click action, skipping",diff --git a/tests/unit/test_blocked_form_advance_followup.py b/tests/unit/test_blocked_form_advance_followup.pynew file mode 100644index 000000000..8cee8faba--- /dev/null+++ b/tests/unit/test_blocked_form_advance_followup.py@@ -0,0 +1,134 @@+from types import SimpleNamespace+from unittest.mock import AsyncMock, Mock++import pytest++from skyvern.webeye.actions import handler+from skyvern.webeye.actions.responses import ActionFailure, ActionSuccess+++def _element(tag: str, *, attributes: dict | None = None, text: str | None = None) -> dict:+ return {"tagName": tag, "attributes": attributes or {}, "text": text}+++ ("element", "expected"),+ [+ (_element("button", text="Next"), True),+ (_element("button", text="Continue"), True),+ (_element("button", text="Save and proceed"), True),+ (_element("input", attributes={"type": "submit", "value": "Go"}), True),+ (_element("input", attributes={"type": "image"}), True),+ (_element("a", attributes={"role": "button"}, text="Confirm"), True),+ (_element("button", attributes={"aria-label": "Submit application"}), True),+ (_element("button", text="Add another item"), False),+ (_element("input", attributes={"type": "checkbox"}), False),+ (_element("div", text="Next"), False),+ (_element("a", text="Next"), False),+ (_element("button", text=""), False),+ ],+)+def test_click_is_submit_like(element: dict, expected: bool) -> None:+ assert handler._click_is_submit_like(element) is expected+++def _submit_element() -> object:+ return SimpleNamespace(+ get_element_dict=Mock(return_value=_element("button", text="Next")),+ get_id=Mock(return_value="next-btn"),+ )+++async def test_blocked_form_advance_sets_followup_on_non_advancing_submit(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(handler, "_is_surface_blocked_form_advance_enabled", AsyncMock(return_value=True))+ results: list[object] = [ActionSuccess()]+ await handler._maybe_flag_blocked_form_advance(+ results=results,+ skyvern_element=_submit_element(),+ page=SimpleNamespace(url="https://example.com/step"),+ original_url="https://example.com/step",+ incremental_scraped=SimpleNamespace(get_incremental_elements_num=AsyncMock(return_value=0)),+ task=SimpleNamespace(organization_id=None),+ )+ assert results[-1].needs_followup is True+ assert results[-1].followup_message+ assert "Next" in results[-1].followup_message+++async def test_blocked_form_advance_noop_when_url_changed(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(handler, "_is_surface_blocked_form_advance_enabled", AsyncMock(return_value=True))+ results: list[object] = [ActionSuccess()]+ await handler._maybe_flag_blocked_form_advance(+ results=results,+ skyvern_element=_submit_element(),+ page=SimpleNamespace(url="https://example.com/next-step"),+ original_url="https://example.com/step",+ incremental_scraped=SimpleNamespace(get_incremental_elements_num=AsyncMock(return_value=0)),+ task=SimpleNamespace(organization_id=None),+ )+ assert results[-1].needs_followup is None+++async def test_blocked_form_advance_noop_when_new_elements_appeared(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(handler, "_is_surface_blocked_form_advance_enabled", AsyncMock(return_value=True))+ results: list[object] = [ActionSuccess()]+ await handler._maybe_flag_blocked_form_advance(+ results=results,+ skyvern_element=_submit_element(),+ page=SimpleNamespace(url="https://example.com/step"),+ original_url="https://example.com/step",+ incremental_scraped=SimpleNamespace(get_incremental_elements_num=AsyncMock(return_value=5)),+ task=SimpleNamespace(organization_id=None),+ )+ assert results[-1].needs_followup is None+++async def test_blocked_form_advance_noop_when_not_submit_like(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(handler, "_is_surface_blocked_form_advance_enabled", AsyncMock(return_value=True))+ results: list[object] = [ActionSuccess()]+ await handler._maybe_flag_blocked_form_advance(+ results=results,+ skyvern_element=SimpleNamespace(+ get_element_dict=Mock(return_value=_element("input", attributes={"type": "checkbox"})),+ get_id=Mock(return_value="cb"),+ ),+ page=SimpleNamespace(url="https://example.com/step"),+ original_url="https://example.com/step",+ incremental_scraped=SimpleNamespace(get_incremental_elements_num=AsyncMock(return_value=0)),+ task=SimpleNamespace(organization_id=None),+ )+ assert results[-1].needs_followup is None+++async def test_blocked_form_advance_noop_when_flag_disabled(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(handler, "_is_surface_blocked_form_advance_enabled", AsyncMock(return_value=False))+ results: list[object] = [ActionSuccess()]+ await handler._maybe_flag_blocked_form_advance(+ results=results,+ skyvern_element=_submit_element(),+ page=SimpleNamespace(url="https://example.com/step"),+ original_url="https://example.com/step",+ incremental_scraped=SimpleNamespace(get_incremental_elements_num=AsyncMock(return_value=0)),+ task=SimpleNamespace(organization_id=None),+ )+ assert results[-1].needs_followup is None+++async def test_blocked_form_advance_noop_when_last_result_not_success(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(handler, "_is_surface_blocked_form_advance_enabled", AsyncMock(return_value=True))+ results: list[object] = [ActionFailure(Exception("boom"))]+ await handler._maybe_flag_blocked_form_advance(+ results=results,+ skyvern_element=_submit_element(),+ page=SimpleNamespace(url="https://example.com/step"),+ original_url="https://example.com/step",+ incremental_scraped=SimpleNamespace(get_incremental_elements_num=AsyncMock(return_value=0)),+ task=SimpleNamespace(organization_id=None),+ )+ assert results[-1].needs_followup is None