🕷️ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev
Python aiai-scrapingautomationcrawlercrawlingcrawling-pythondatadata-extractionmcpmcp-serverplaywrightpythonscrapingselectorsstealthweb-scraperweb-scrapingweb-scraping-pythonwebscrapingxpath
feat(mcp): Add a dialog handling action
Karim shoair7d97d0727660
diff --git a/agent-skill/Scrapling-Skill.zip b/agent-skill/Scrapling-Skill.zipindex 1dc147d..2d7d744 100644Binary files a/agent-skill/Scrapling-Skill.zip and b/agent-skill/Scrapling-Skill.zip differdiff --git a/agent-skill/Scrapling-Skill/references/mcp-server.md b/agent-skill/Scrapling-Skill/references/mcp-server.mdindex 006cd61..1919130 100644--- a/agent-skill/Scrapling-Skill/references/mcp-server.md+++ b/agent-skill/Scrapling-Skill/references/mcp-server.md@@ -149,9 +149,9 @@ Search applies to each line of the captured snapshot, after the `depth` limit. R{"session_id": "browser", "search": "Next"}```-### `browser_actions` -- Chain mouse, field, keyboard, and wait actions+### `browser_actions` -- Chain mouse, field, keyboard, dialog, and wait actions-Runs actions in order on the existing page in a stealthy browser session. Call `browser_fetch` first. The flat `actions` list can mix any of the eleven action types below; use `browser_snapshot` when you need to inspect the page before choosing later actions.+Runs actions in order on the existing page in a stealthy browser session. Call `browser_fetch` first. The flat `actions` list can mix any of the twelve action types below; use `browser_snapshot` when you need to inspect the page before choosing later actions.| Parameter | Type | Default | Description ||-----------|------|---------|-------------|@@ -169,13 +169,14 @@ Runs actions in order on the existing page in a stealthy browser session. Call `| `radio` | Target, `value`, `timeout=30000` | Select the radio option; `value` is required and only `true` is supported || `combobox` | Target, `value`, `timeout=30000` | Select native `<select>` options by label; use a string, a list of strings for multiple selections, or `[]` to clear || `press_key` | `key` | Press a nonempty native key name, character, or shortcut at the current focus |+| `dialog` | `accept`, optional `prompt_text` | Queue one reply for the next browser dialog; `true` accepts, `false` dismisses || `wait_time` | `milliseconds` | Fixed pause in finite nonnegative milliseconds || `wait_element` | `target`, `state="visible"`, `timeout=30000` | Wait for an element to reach `"visible"`, `"hidden"`, `"attached"`, or `"detached"` || `wait_load` | `state`, `timeout=30000` | Wait for `"domcontentloaded"`, `"load"`, or `"networkidle"` |**Targets and timing.** Element actions use a nonempty `target`: a Playwright selector such as `"#search"` or a snapshot reference as `"aria-ref=e2"`. Bare reference strings such as `"e2"` are treated as selectors. Each move or click needs either `target` or both `x` and `y`. Coordinates must be finite viewport CSS pixels from the main frame's top-left, not full-page screenshot coordinates. Field actions need `target` and can target fields anywhere on the page without a `<form>` parent. Element waits also use `target`. Mouse and field targets must match exactly one element. Use current snapshot references; do not build a chain with references that need a future snapshot.-Each action with a `timeout` accepts finite nonnegative milliseconds, default 30000; 0 disables the limit. Mouse coordinate actions ignore it. For text input, the limit applies separately to each native operation, including clearing and each character press in slow mode. Keyboard, wheel, and fixed-pause actions have no timeout option. Pauses between actions are outside action timeouts.+Each action with a `timeout` accepts finite nonnegative milliseconds, default 30000; 0 disables the limit. Mouse coordinate actions ignore it. For text input, the limit applies separately to each native operation, including clearing and each character press in slow mode. Keyboard, dialog, wheel, and fixed-pause actions have no timeout option. Pauses between actions are outside action timeouts.**Mouse actions.** Element targets use native locator hover or click, which waits for readiness and scrolls into view. Coordinate actions use the native mouse without automatic waiting or scrolling. Coordinate clicks move the mouse, then press and release, without waiting for navigation. Wheel actions neither move the pointer nor wait for scrolling, animations, or loaded content to finish. The page may prevent scrolling, or the scrollable area may be at its edge.@@ -183,6 +184,12 @@ Each action with a `timeout` accepts finite nonnegative milliseconds, default 30Each `press_key` uses native `page.keyboard.press` at the current focus. Keep a shortcut such as `"ControlOrMeta+A"` in one `key` string, then use another `press_key` action with `"Backspace"` to delete. A space (`" "`) and plus (`"+"`) are valid key strings. The native API validates key names and shortcuts when pressed. Use a click action to focus the intended control before an Enter press when needed. Key presses do not wait for navigation.+**Dialogs.** Place `dialog` before the click, key, or other action that opens an alert, confirmation, prompt, or leave-page warning (`beforeunload`). Each reply handles one dialog; queue several replies before a trigger that opens several dialogs. Replies are used in order. For prompts, `prompt_text` supplies the accepted text; omitting it or passing an empty string returns empty text, even if the prompt has a default. It has no effect when dismissing or accepting other dialog types. Unused replies expire when the call ends; the action does not wait for a dialog by itself. For delayed dialogs, include a wait for the resulting page change in the same chain. HTML modal windows use normal element actions.++```json+{"session_id": "browser", "actions": [{"type": "dialog", "accept": true, "prompt_text": "books"}, {"type": "click", "target": "#open-prompt"}]}+```+**Waits.** Element waits are strict: multiple matches return an error. `attached` means present in the DOM, and `detached` means absent. `visible` requires a nonempty bounding box and no `visibility:hidden`; `hidden` also succeeds when the element is absent. Load waits observe the current committed document and return immediately if the state was already reached. A click or key press followed by `wait_load` does not guarantee waiting for delayed future navigation. `domcontentloaded` waits for DOMContentLoaded, not all future JavaScript work; `load` waits for the load event; `networkidle` requires no active network connections for at least 500 ms and does not prove application readiness. Prefer a specific result element when it signals readiness.For example, fill and submit a search, wait for its results, then hover over the results panel and scroll it:diff --git a/docs/ai/mcp-server.md b/docs/ai/mcp-server.mdindex dad8078..d279576 100644--- a/docs/ai/mcp-server.md+++ b/docs/ai/mcp-server.md@@ -38,7 +38,7 @@ The Scrapling MCP Server provides fourteen tools for web scraping and browser in#### Browser Actions-- **`browser_actions`**: Chain mouse actions, field filling, keyboard shortcuts, and waits in one call. For example, fill a search box, submit it, wait for results, and open a result. The AI can combine known steps while keeping snapshots separate when it needs to inspect a page change.+- **`browser_actions`**: Chain mouse actions, field filling, keyboard shortcuts, dialog replies, and waits in one call. Accept or dismiss browser alerts and confirmations, or enter text in prompts. For example, fill a search box, submit it, wait for results, and open a result. The AI can combine known steps while keeping snapshots separate when it needs to inspect a page change.Reveal hover menus and tooltips, click buttons and links, or move over a nested panel and scroll it vertically or horizontally. Moves and clicks support selectors, snapshot references, or coordinates, with automatic waiting and scrolling into view for element targets. Supports coordinate movement in multiple steps, left, right, and middle clicks, plus double-clicks.diff --git a/docs/api-reference/mcp-server.md b/docs/api-reference/mcp-server.mdindex b1e3e17..3b8419b 100644--- a/docs/api-reference/mcp-server.md+++ b/docs/api-reference/mcp-server.md@@ -78,7 +78,7 @@ The main MCP server class that provides all web scraping tools:- **`browser_fetch_once`**: Fetch one URL through a stealthy browser with JavaScript rendering and Cloudflare handling, then close the browser. Use a persistent session for later actions or network history.- **`browser_snapshot`**: Inspect the current page without reloading it. Structured text includes element roles, names, references, positions, and sizes to help the AI target page controls. Search for text to return matching lines with nearby context and parent nodes, making large pages easier to inspect.-- **`browser_actions`**: Chain mouse moves, hovers, clicks, scrolling, field filling, keyboard shortcuts, and waits in one call. Fill a search box, submit it, wait for results, and open a result without a separate call for each step.+- **`browser_actions`**: Chain mouse moves, hovers, clicks, scrolling, field filling, keyboard shortcuts, dialog replies, and waits in one call. Accept or dismiss alerts and confirmations, or enter text in prompts. Fill a search box, submit it, wait for results, and open a result without a separate call for each step.Moves and clicks target selectors, snapshot references, or coordinates; element targets wait and scroll into view. Supports movement in multiple steps, left, right, and middle clicks, double-clicks, and scrolling nested panels.diff --git a/scrapling/core/ai/_browser_actions.py b/scrapling/core/ai/_browser_actions.pyindex a8a18e7..cac1e23 100644--- a/scrapling/core/ai/_browser_actions.py+++ b/scrapling/core/ai/_browser_actions.py@@ -1,5 +1,6 @@from random import uniform-from asyncio import CancelledError, sleep+from asyncio import CancelledError, Task, create_task, gather, shield, sleep+from collections import dequefrom anyio import CancelScopefrom pydantic import Field, FiniteFloat, PositiveInt@@ -99,6 +100,12 @@ class _KeyPress(TypedDict):]+class _DialogAction(TypedDict):+ type: Literal["dialog"]+ accept: Annotated[bool, Field(description="Queue one reply before its trigger; unused replies expire at call end.")]+ prompt_text: NotRequired[Annotated[str, Field(description="Text for an accepted prompt; omitted means empty.")]]++class _FormTarget(TypedDict, total=False):target: _Targettimeout: Annotated[_FieldTimeout, Field(default=30000)]@@ -137,6 +144,7 @@ BrowserAction = Annotated[_RadioFormField,_SelectFormField,_KeyPress,+ _DialogAction,_TimeWait,_ElementWait,_LoadWait,@@ -159,64 +167,108 @@ def _validate_actions(actions: List[BrowserAction]) -> None:async def _run_actions(page: Any, actions: List[BrowserAction], slowly: bool = False) -> None:"""Run an ordered action chain on a reserved browser page."""- for index, action in enumerate(actions, 1):+ replies: deque[tuple[int, _DialogAction]] = deque()+ handlers: List[Task[None]] = []+ dialog_error: Optional[RuntimeError] = None++ async def reply(dialog: Any, index: int, action: _DialogAction) -> None:+ nonlocal dialog_errortry:- if slowly and index > 1:- await sleep(uniform(0.1, 0.3))- if action["type"] == "wait_time":- await page.wait_for_timeout(action["milliseconds"])- elif action["type"] == "wait_element":- await page.locator(action["target"]).wait_for(- state=action.get("state", "visible"), timeout=action.get("timeout", 30000)- )- elif action["type"] == "wait_load":- await page.wait_for_load_state(action["state"], timeout=action.get("timeout", 30000))- elif action["type"] == "press_key":- await page.keyboard.press(action["key"])- elif action["type"] == "wheel":- await page.mouse.wheel(action.get("delta_x", 0), action.get("delta_y", 0))- else:- target = action.get("target")- timeout = action.get("timeout", 30000)- if action["type"] == "move" or action["type"] == "click":- locator = page.locator(target) if target is not None else None- if action["type"] == "move":- if locator is not None:- await locator.hover(timeout=timeout)- else:- await page.mouse.move(action.get("x"), action.get("y"), steps=action.get("steps", 1))- else:- options = {- "button": action.get("button", "left"),- "click_count": action.get("click_count", 1),- "delay": action.get("delay", 0),- }- try:- if locator is not None:- await locator.click(**options, timeout=timeout)- else:- await page.mouse.click(action.get("x"), action.get("y"), **options)- except (CancelledError, PatchrightTimeoutError) as exc:- try:- with CancelScope(shield=True):- if not page.is_closed():- await page.mouse.up(button=options["button"])- except Exception as cleanup_error:- raise exc from cleanup_error- raise+ with CancelScope(shield=True):+ if action["accept"]:+ await dialog.accept(prompt_text=action.get("prompt_text"))else:- locator = page.locator(target)- if action["type"] == "textbox":- if action.get("clear", True):- await locator.fill("" if slowly else action["value"], timeout=timeout)- if slowly and action["value"]:- for character in action["value"]:- await locator.press_sequentially(character, delay=uniform(50, 150), timeout=timeout)- elif not action.get("clear", True):- await locator.press_sequentially(action["value"], timeout=timeout)- elif action["type"] == "combobox":- await locator.select_option(label=action["value"], timeout=timeout)- else:- await locator.set_checked(action["value"], timeout=timeout)+ await dialog.dismiss()except Exception as exc:- raise RuntimeError(f"Action {index} ({action['type']}) failed: {exc}") from exc+ if dialog_error is None:+ dialog_error = RuntimeError(f"Action {index} (dialog) failed: {exc}")+ dialog_error.__cause__ = exc+ dialog_scope.cancel()++ def handle_dialog(dialog: Any) -> None:+ index, action = replies.popleft()+ if not replies:+ page.remove_listener("dialog", handle_dialog)+ handlers.append(create_task(reply(dialog, index, action)))++ with CancelScope() as dialog_scope:+ try:+ for index, action in enumerate(actions, 1):+ try:+ if slowly and index > 1:+ await sleep(uniform(0.1, 0.3))+ if action["type"] == "dialog":+ if not replies:+ page.on("dialog", handle_dialog)+ replies.append((index, action))+ elif action["type"] == "wait_time":+ await page.wait_for_timeout(action["milliseconds"])+ elif action["type"] == "wait_element":+ await page.locator(action["target"]).wait_for(+ state=action.get("state", "visible"), timeout=action.get("timeout", 30000)+ )+ elif action["type"] == "wait_load":+ await page.wait_for_load_state(action["state"], timeout=action.get("timeout", 30000))+ elif action["type"] == "press_key":+ await page.keyboard.press(action["key"])+ elif action["type"] == "wheel":+ await page.mouse.wheel(action.get("delta_x", 0), action.get("delta_y", 0))+ else:+ target = action.get("target")+ timeout = action.get("timeout", 30000)+ if action["type"] == "move" or action["type"] == "click":+ locator = page.locator(target) if target is not None else None+ if action["type"] == "move":+ if locator is not None:+ await locator.hover(timeout=timeout)+ else:+ await page.mouse.move(+ action.get("x"), action.get("y"), steps=action.get("steps", 1)+ )+ else:+ options = {+ "button": action.get("button", "left"),+ "click_count": action.get("click_count", 1),+ "delay": action.get("delay", 0),+ }+ try:+ if locator is not None:+ await locator.click(**options, timeout=timeout)+ else:+ await page.mouse.click(action.get("x"), action.get("y"), **options)+ except (CancelledError, PatchrightTimeoutError) as exc:+ try:+ with CancelScope(shield=True):+ if not page.is_closed():+ await page.mouse.up(button=options["button"])+ except Exception as cleanup_error:+ raise exc from cleanup_error+ raise+ else:+ locator = page.locator(target)+ if action["type"] == "textbox":+ if action.get("clear", True):+ await locator.fill("" if slowly else action["value"], timeout=timeout)+ if slowly and action["value"]:+ for character in action["value"]:+ await locator.press_sequentially(+ character, delay=uniform(50, 150), timeout=timeout+ )+ elif not action.get("clear", True):+ await locator.press_sequentially(action["value"], timeout=timeout)+ elif action["type"] == "combobox":+ await locator.select_option(label=action["value"], timeout=timeout)+ else:+ await locator.set_checked(action["value"], timeout=timeout)+ for handler in handlers:+ await shield(handler)+ handlers.clear()+ except Exception as exc:+ raise RuntimeError(f"Action {index} ({action['type']}) failed: {exc}") from exc+ finally:+ if replies:+ page.remove_listener("dialog", handle_dialog)+ with CancelScope(shield=True):+ await gather(*handlers)+ if dialog_error is not None:+ raise dialog_errordiff --git a/scrapling/core/ai/server.py b/scrapling/core/ai/server.pyindex 83de07e..a22bb11 100644--- a/scrapling/core/ai/server.py+++ b/scrapling/core/ai/server.py@@ -491,7 +491,7 @@ class ScraplingMCPServer:actions: Annotated[List[BrowserAction], Field(min_length=1)],slowly: bool = False,) -> str:- """Run mouse, field, key, and wait actions in order; return plain text without a snapshot.+ """Run mouse, field, key, dialog, and wait actions in order; return plain text without a snapshot.Stop on the first error, identifying its action; partial effects remain. No retries.Element mouse targets auto-wait and scroll into view; coordinates and wheel do not wait for navigation or scrolling.Cancelled/timed-out clicks attempt button release. Load waits observe the current document, not future navigation.@@ -910,7 +910,7 @@ class ScraplingMCPServer:5. Session options persist from opening; fetch options apply per call, with schema defaults.6. Each browser session uses one page; finish each call before starting the next in that session.7. Snapshots include refs and boxes by default; `main_content_only` and `pierce_shadow` do not filter them.-8. `browser_actions` chains mouse, field, key and wait actions on the current page. Element actions use `target`: a Playwright selector or `aria-ref=<ref>` from the current snapshot. Move/click can instead use viewport (x, y) in CSS pixels. Wheel uses the current pointer without waiting for scrolling; coordinate clicks do not wait for navigation. Load waits observe the current document; wait for a result element for delayed navigation.+8. `browser_actions` chains mouse, field, key, dialog and wait actions on the current page. Element actions use `target`: a Playwright selector or `aria-ref=<ref>` from the current snapshot. Move/click can instead use viewport (x, y) in CSS pixels. Wheel uses the current pointer without waiting for scrolling; coordinate clicks do not wait for navigation. Load waits observe the current document; wait for a result element for delayed navigation.9. Network history saves completed requests and responses across page changes. Failed/unfinished requests are omitted; closing does not wait for running captures. Use `include_static=True` to list successful non-API traffic. Limits: 1000 requests and 20 MiB of saved response bodies; oldest records are removed first. Only response bodies with a text Content-Type are read and saved. Response bodies over 1 MiB are skipped; unavailable bodies are marked.""",}if self._auth_token:diff --git a/tests/ai/test_browser_dialog.py b/tests/ai/test_browser_dialog.pynew file mode 100644index 0000000..499a3fc--- /dev/null+++ b/tests/ai/test_browser_dialog.py@@ -0,0 +1,159 @@+import asyncio+from contextlib import suppress+from unittest.mock import AsyncMock, Mock++import pytest+from anyio import CancelScope+from mcp.client import Client+from mcp.types import TextContent++from scrapling.core.ai import ScraplingMCPServer+from scrapling.core._types import Any+from tests.ai.test_browser_actions import _server+++async def test_dialog_schema() -> None:+ async with Client(ScraplingMCPServer()._build_server("127.0.0.1", 8000)) as client:+ tool = next(tool for tool in (await client.list_tools()).tools if tool.name == "browser_actions")+ mapping = tool.input_schema["properties"]["actions"]["items"]["discriminator"]["mapping"]+ schema = tool.input_schema["$defs"][mapping["dialog"].rsplit("/", 1)[1]]+ assert schema["required"] == ["type", "accept"]+ assert set(schema["properties"]) == {"type", "accept", "prompt_text"}+ assert schema["properties"]["accept"]["type"] == "boolean"+ assert schema["properties"]["prompt_text"]["type"] == "string"+ assert tool.output_schema is None+++ "values",+ [{}, {"accept": None}, {"accept": []}, {"accept": True, "prompt_text": None}, {"accept": True, "prompt_text": 2}],+)+async def test_invalid_dialog_prevents_entire_chain(values: dict[str, Any]) -> None:+ server, session, page = _server()+ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ result = await client.call_tool(+ "browser_actions",+ {"session_id": "browser", "actions": [{"type": "press_key", "key": "Enter"}, {"type": "dialog", **values}]},+ )+ assert result.is_error+ page.on.assert_not_called()+ page.is_closed.assert_not_called()+ page.keyboard.press.assert_not_awaited()+ assert session.page_pool.pages[0].state == "ready"+++async def test_unused_dialog_is_removed_without_waiting() -> None:+ server, session, page = _server()+ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ result = await client.call_tool(+ "browser_actions", {"session_id": "browser", "actions": [{"type": "dialog", "accept": True}]}+ )+ assert not result.is_error+ assert result.content == [TextContent(type="text", text="Actions completed.")]+ page.on.assert_called_once()+ page.remove_listener.assert_called_once_with(*page.on.call_args.args)+ assert session.page_pool.pages[0].state == "ready"++[email protected]("accept", [True, False])[email protected]("trigger", ["click", "press_key"])[email protected]("blocked", [False, True])+async def test_dialog_failure_stops_blocked_action_and_reaches_mcp(accept: bool, trigger: str, blocked: bool) -> None:+ server, session, page = _server()+ dialog = Mock(accept=AsyncMock(), dismiss=AsyncMock())+ method = dialog.accept if accept else dialog.dismiss+ method.side_effect = RuntimeError("reply failed")+ page.mouse.up = AsyncMock()++ async def open_dialog(*args: Any, **kwargs: Any) -> None:+ page.on.call_args.args[1](dialog)+ if blocked:+ await asyncio.Event().wait()++ native = page.keyboard.press if trigger == "press_key" else page.locator.return_value.click+ if trigger == "click":+ page.locator.return_value.click = native = AsyncMock()+ native.side_effect = open_dialog+ action = {"type": "press_key", "key": "Enter"} if trigger == "press_key" else {"type": "click", "target": "button"}+ async with Client(server._build_server("127.0.0.1", 8000)) as client:+ result = await asyncio.wait_for(+ client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "actions": [{"type": "dialog", "accept": accept}, action, {"type": "wheel", "delta_y": 1}],+ },+ ),+ 5,+ )+ assert result.is_error+ assert "Action 1 (dialog) failed: reply failed" in str(result.content)+ method.assert_awaited_once()+ page.remove_listener.assert_called_once_with(*page.on.call_args.args)+ page.mouse.wheel.assert_not_awaited()+ assert page.mouse.up.await_count == (1 if trigger == "click" and blocked else 0)+ assert session.page_pool.pages[0].state == "ready"++[email protected]("cancel_mode", ["task", "scope"])[email protected]("blocked", [False, True])+async def test_cancellation_waits_for_active_reply_before_releasing_page(cancel_mode: str, blocked: bool) -> None:+ server, session, page = _server()+ entered, release, cancelled = asyncio.Event(), asyncio.Event(), asyncio.Event()+ scope = CancelScope()+ dialog = Mock(accept=AsyncMock(), dismiss=AsyncMock())++ async def accept(**kwargs: Any) -> None:+ entered.set()+ await release.wait()++ async def open_dialog(key: str) -> None:+ page.on.call_args.args[1](dialog)+ try:+ if blocked:+ await asyncio.Event().wait()+ finally:+ cancelled.set()++ async def run() -> None:+ with scope:+ await server.browser_actions(+ "browser",+ [+ {"type": "dialog", "accept": True},+ {"type": "press_key", "key": "Enter"},+ {"type": "wheel", "delta_y": 1},+ ],+ )++ dialog.accept.side_effect = accept+ page.keyboard.press.side_effect = open_dialog+ task = asyncio.create_task(run())+ try:+ await asyncio.wait_for(entered.wait(), 5)+ scope.cancel() if cancel_mode == "scope" else task.cancel()+ await asyncio.wait_for(cancelled.wait(), 5)+ assert session.page_pool.pages[0].state == "busy"+ assert not task.done()+ release.set()+ if cancel_mode == "task":+ with pytest.raises(asyncio.CancelledError):+ await asyncio.wait_for(task, 5)+ else:+ await asyncio.wait_for(task, 5)+ assert scope.cancelled_caught+ finally:+ release.set()+ if not task.done():+ task.cancel()+ with suppress(asyncio.CancelledError):+ await task+ dialog.accept.assert_awaited_once_with(prompt_text=None)+ page.remove_listener.assert_called_once_with(*page.on.call_args.args)+ page.mouse.wheel.assert_not_awaited()+ assert session.page_pool.pages[0].state == "ready"diff --git a/tests/ai/test_browser_dialog_live.py b/tests/ai/test_browser_dialog_live.pynew file mode 100644index 0000000..fa95af4--- /dev/null+++ b/tests/ai/test_browser_dialog_live.py@@ -0,0 +1,264 @@+import asyncio+from json import loads++import pytest+from mcp.types import TextContent++from scrapling.core._types import Any+from tests.ai.test_browser_wait import _browser+++ "expression, reply, expected",+ [+ ("alert('Notice')", {"accept": True}, None),+ ("alert('Notice')", {"accept": False}, None),+ ("confirm('Continue?')", {"accept": True}, True),+ ("confirm('Continue?')", {"accept": False}, False),+ ("prompt('Name?', 'Original')", {"accept": True}, ""),+ ("prompt('Name?', 'Original')", {"accept": True, "prompt_text": ""}, ""),+ ("prompt('Name?', 'Original')", {"accept": True, "prompt_text": "مرحبا 🌍"}, "مرحبا 🌍"),+ ("prompt('Name?', 'Original')", {"accept": False}, None),+ ("prompt('Name?', 'Original')", {"accept": False, "prompt_text": "Ignored"}, None),+ ],+)+async def test_dialog_live_native_results(expression: str, reply: dict[str, Any], expected: Any) -> None:+ async with _browser() as (client, session, page):+ await page.set_content("<button id=trigger>Open</button>")+ await page.evaluate(f"""() => {{+ document.querySelector('#trigger').onclick = () => {{+ document.body.dataset.result = JSON.stringify({expression}) ?? 'null';+ document.body.dataset.done = 'yes';+ }};+ }}""")+ if expression.startswith("prompt") and reply == {"accept": True}:+ page.once("dialog", lambda dialog: dialog.accept())+ await page.locator("#trigger").click(timeout=5000)+ assert loads(await page.get_attribute("body", "data-result")) == expected+ result = await client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "actions": [{"type": "dialog", **reply}, {"type": "click", "target": "#trigger", "timeout": 5000}],+ },+ )+ assert not result.is_error and result.structured_content is None+ assert result.content == [TextContent(type="text", text="Actions completed.")]+ assert await page.evaluate("document.body.dataset.done") == "yes"+ assert loads(await page.get_attribute("body", "data-result")) == expected+ assert session.page_pool.pages[0].state == "ready"++[email protected]("trigger", ["selector", "coordinates", "keyboard"])+async def test_dialog_live_queued_replies_and_unexpected_dialog(trigger: str) -> None:+ async with _browser() as (client, session, page):+ await page.set_content("""<button id=trigger>Open</button><script>+ document.querySelector('#trigger').onclick = () => {+ document.body.dataset.results = JSON.stringify([confirm('Continue?'), prompt('Name?', 'Original'), confirm('Unexpected?')]);+ };+ </script>""")+ click: dict[str, Any] = {"type": "click", "target": "#trigger", "timeout": 5000}+ if trigger == "coordinates":+ box = await page.locator("#trigger").bounding_box()+ assert box is not None+ click = {"type": "click", "x": box["x"] + box["width"] / 2, "y": box["y"] + box["height"] / 2}+ elif trigger == "keyboard":+ await page.locator("#trigger").focus()+ click = {"type": "press_key", "key": "Enter"}+ result = await client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "actions": [+ {"type": "dialog", "accept": True},+ {"type": "dialog", "accept": True, "prompt_text": "Scrapling"},+ click,+ ],+ },+ )+ assert not result.is_error+ assert loads(await page.get_attribute("body", "data-results")) == [True, "Scrapling", False]+ assert session.page_pool.pages[0].state == "ready"++[email protected]("slowly", [False, True])+async def test_dialog_live_new_reply_after_previous_trigger(slowly: bool) -> None:+ async with _browser() as (client, session, page):+ await page.set_content("""<button id=trigger>Open</button><script>+ window.results = [];+ document.querySelector('#trigger').onclick = () => {+ window.results.push(confirm('Continue?'));+ document.body.dataset.results = JSON.stringify(window.results);+ };+ </script>""")+ result = await client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "slowly": slowly,+ "actions": [+ {"type": "dialog", "accept": True},+ {"type": "click", "target": "#trigger", "timeout": 5000},+ {"type": "dialog", "accept": False},+ {"type": "click", "target": "#trigger", "timeout": 5000},+ ],+ },+ )+ assert not result.is_error+ assert loads(await page.get_attribute("body", "data-results")) == [True, False]+ assert session.page_pool.pages[0].state == "ready"+++async def test_dialog_live_keyboard_trigger() -> None:+ async with _browser() as (client, session, page):+ await page.set_content("""<input id=name><script>+ document.querySelector('#name').onkeydown = event => {+ if (event.key === 'Enter') document.body.dataset.result = JSON.stringify(prompt('Name?', 'Original'));+ };+ </script>""")+ result = await client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "actions": [+ {"type": "textbox", "target": "#name", "value": "Query", "timeout": 5000},+ {"type": "dialog", "accept": True, "prompt_text": "Answer"},+ {"type": "press_key", "key": "Enter"},+ ],+ },+ )+ assert not result.is_error+ assert loads(await page.get_attribute("body", "data-result")) == "Answer"+ assert session.page_pool.pages[0].state == "ready"+++async def test_dialog_live_delayed_trigger_during_wait() -> None:+ async with _browser() as (client, session, page):+ await page.set_content("""<button id=trigger>Open</button><p id=done hidden>Done</p><script>+ document.querySelector('#trigger').onclick = () => setTimeout(() => {+ document.body.dataset.result = JSON.stringify(confirm('Continue?'));+ document.querySelector('#done').hidden = false;+ }, 100);+ </script>""")+ result = await client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "actions": [+ {"type": "dialog", "accept": True},+ {"type": "click", "target": "#trigger", "timeout": 5000},+ {"type": "wait_element", "target": "#done", "timeout": 5000},+ ],+ },+ )+ assert not result.is_error+ assert loads(await page.get_attribute("body", "data-result")) is True+ assert await page.locator("#done").is_visible()+ assert session.page_pool.pages[0].state == "ready"++[email protected]("accept", [False, True])+async def test_dialog_live_beforeunload_navigation(accept: bool) -> None:+ async with _browser() as (client, session, page):+ await page.set_content("""<button id=activate>Start</button><a id=next href=/next>Next</a><script>+ addEventListener('beforeunload', event => {+ event.preventDefault();+ event.returnValue = '';+ });+ </script>""")+ seen: list[str] = []++ def record(dialog: Any) -> None:+ seen.append(dialog.type)++ page.on("dialog", record)+ try:+ result = await client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "actions": [+ {"type": "click", "target": "#activate", "timeout": 5000},+ {"type": "dialog", "accept": accept},+ {"type": "click", "target": "#next", "timeout": 5000},+ ],+ },+ )+ finally:+ page.remove_listener("dialog", record)+ assert not result.is_error+ assert seen == ["beforeunload"]+ assert page.url == ("https://wait.test/next" if accept else "https://wait.test/")+ assert await page.locator("#activate").count() == (0 if accept else 1)+ assert session.page_pool.pages[0].state == "ready"++[email protected]("fail", [False, True])+async def test_dialog_live_unused_replies_do_not_reach_later_calls(fail: bool) -> None:+ async with _browser() as (client, session, page):+ await page.set_content("""<button id=trigger>Open</button><script>+ document.querySelector('#trigger').onclick = () => { document.body.dataset.result = JSON.stringify(confirm('Continue?')); };+ </script>""")+ actions: list[dict[str, Any]] = [{"type": "dialog", "accept": True}]+ if fail:+ actions.append({"type": "wait_element", "target": "#missing", "timeout": 50})+ result = await client.call_tool("browser_actions", {"session_id": "browser", "actions": actions})+ assert bool(result.is_error) == fail+ if fail:+ assert isinstance(result.content[0], TextContent)+ assert "Action 2 (wait_element) failed:" in result.content[0].text+ assert session.page_pool.pages[0].state == "ready"+ result = await client.call_tool(+ "browser_actions",+ {"session_id": "browser", "actions": [{"type": "click", "target": "#trigger", "timeout": 5000}]},+ )+ assert not result.is_error+ assert loads(await page.get_attribute("body", "data-result")) is False+++async def test_dialog_live_cancel_removes_unused_reply() -> None:+ async with _browser() as (client, session, page):+ await page.set_content("""<button id=start>Start</button><button id=trigger>Open</button><script>+ document.querySelector('#start').onclick = () => { document.body.dataset.started = 'yes'; };+ document.querySelector('#trigger').onclick = () => { document.body.dataset.result = JSON.stringify(confirm('Continue?')); };+ </script>""")+ task = asyncio.create_task(+ client.call_tool(+ "browser_actions",+ {+ "session_id": "browser",+ "actions": [+ {"type": "dialog", "accept": True},+ {"type": "click", "target": "#start", "timeout": 5000},+ {"type": "wait_element", "target": "#missing", "timeout": 0},+ ],+ },+ )+ )+ try:+ await page.wait_for_function("document.body.dataset.started === 'yes'", timeout=5000)+ assert session.page_pool.pages[0].state == "busy"+ finally:+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task++ async def released() -> None:+ while session.page_pool.pages[0].state != "ready":+ await asyncio.sleep(0.01)++ await asyncio.wait_for(released(), 5)+ result = await client.call_tool(+ "browser_actions",+ {"session_id": "browser", "actions": [{"type": "click", "target": "#trigger", "timeout": 5000}]},+ )+ assert not result.is_error+ assert loads(await page.get_attribute("body", "data-result")) is Falsediff --git a/tests/ai/test_browser_fill_fields.py b/tests/ai/test_browser_fill_fields.pyindex 1b2b234..001f180 100644--- a/tests/ai/test_browser_fill_fields.py+++ b/tests/ai/test_browser_fill_fields.py@@ -79,11 +79,12 @@ async def test_browser_actions_fields_schema() -> None:"click","wheel","press_key",+ "dialog","wait_time","wait_element","wait_load",}- assert len(items["oneOf"]) == 11+ assert len(items["oneOf"]) == 12for kind in ("textbox", "checkbox", "radio", "combobox"):variant = variants[kind]assert set(variant["required"]) == {"type", "value"}diff --git a/tests/ai/test_browser_mouse.py b/tests/ai/test_browser_mouse.pyindex 17c5a83..31a0c40 100644--- a/tests/ai/test_browser_mouse.py+++ b/tests/ai/test_browser_mouse.py@@ -87,8 +87,9 @@ async def test_browser_mouse_schema_and_annotations() -> None:"radio","combobox","press_key",+ "dialog",}- assert len(items["oneOf"]) == 11+ assert len(items["oneOf"]) == 12assert "_Selector" not in schema["$defs"] and "_Ref" not in schema["$defs"]for kind in ("move", "click", "wheel"):variant = variants[kind]diff --git a/tests/ai/test_browser_wait.py b/tests/ai/test_browser_wait.pyindex 35cf45d..7e5b664 100644--- a/tests/ai/test_browser_wait.py+++ b/tests/ai/test_browser_wait.py@@ -58,8 +58,9 @@ async def test_browser_wait_schema_and_annotations() -> None:"radio","combobox","press_key",+ "dialog",}- assert len(items["oneOf"]) == 11+ assert len(items["oneOf"]) == 12assert variants["wait_time"]["required"] == ["type", "milliseconds"]assert variants["wait_time"]["properties"]["milliseconds"]["minimum"] == 0assert variants["wait_element"]["required"] == ["type", "target"]