click_element
Click elements on web pages using CSS selectors to interact with buttons, links, or other interactive components in automated browser sessions.
Instructions
Click an element on the current page.
Args:
selector: CSS selector for the element to click
context: Optional context object for logging (ignored)
Returns:
Confirmation message or error details
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | Yes | ||
| context | No |
Implementation Reference
- src/mcp_web_browser/server.py:169-201 (handler)The core handler function for the 'click_element' tool. It is decorated with @mcp.tool() which serves as the registration mechanism in this MCP implementation. The function locates an element by CSS selector on the current browser page and clicks it, returning a success message or raising an error.@mcp.tool() async def click_element( selector: str, context: Optional[Any] = None ) -> str: """ Click an element on the current page. Args: selector: CSS selector for the element to click context: Optional context object for logging (ignored) Returns: Confirmation message or error details """ global _current_page if not _current_page: raise ValueError("No page is currently loaded. Use browse_to first.") try: element = await _current_page.query_selector(selector) if not element: raise ValueError(f"No element found with selector: {selector}") await element.click() print(f"Clicked element: {selector}", file=sys.stderr) return f"Successfully clicked element: {selector}" except Exception as e: print(f"Error clicking element: {e}", file=sys.stderr) raise ValueError(f"Error clicking element: {e}")