type_text
Type text into a specified web element by providing a locator strategy, value, and text. Optionally clear the element first. Uses Selenium to automate input for browser testing.
Instructions
Type text into an element, optionally clearing it first.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| strategy | Yes | ||
| value | Yes | ||
| text | Yes | ||
| clear_first | No | ||
| timeout_seconds | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The core handler logic: locates an element via strategy/value, optionally clears it, types the text via send_keys, and returns a summary of the element.
def type_text( self, strategy: LocatorStrategy, value: str, text: str, clear_first: bool = True, timeout_seconds: float | None = None, ) -> ElementSummary: with self._lock: element = self._wait_for_element(strategy, value, timeout_seconds) if clear_first: element.clear() element.send_keys(text) return self._summarize_element(element) - src/selenium_mcp_server/server.py:127-136 (registration)Registers the tool via @mcp.tool() decorator, defines the public signature, docstring, and delegates to BrowserManager.type_text via the _run helper.
@mcp.tool() def type_text( strategy: LocatorStrategy, value: str, text: str, clear_first: bool = True, timeout_seconds: float | None = None, ) -> dict[str, Any]: """Type text into an element, optionally clearing it first.""" return _run("type_text", browser.type_text, strategy, value, text, clear_first, timeout_seconds) - The type_text tool uses LocatorStrategy from models (a Union type like 'id' | 'css selector' | 'xpath' | etc.) for its 'strategy' parameter.
from selenium_mcp_server.models import LocatorStrategy logger = logging.getLogger(__name__) settings = get_settings() browser = BrowserManager(settings) mcp = FastMCP("selenium-mcp-server", json_response=True) def _as_dict(value: Any) -> Any: if hasattr(value, "model_dump"): - The _run helper wraps the handler call with error handling, converting results to dicts via _as_dict.
def _run(action: str, func: Any, *args: Any, **kwargs: Any) -> Any: try: return _as_dict(func(*args, **kwargs)) except BrowserError: logger.exception("Browser action failed: %s", action) raise except Exception as exc: logger.exception("Unexpected Selenium MCP error during %s", action) raise BrowserError(f"{action} failed: {exc}") from exc