click_element
Execute clicks on web elements using CSS selectors to automate interactions within a headless browser environment via MCP Web Browser Server.
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
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | ||
| selector | Yes |
Input Schema (JSON Schema)
{
"properties": {
"context": {
"anyOf": [
{},
{
"type": "null"
}
],
"default": null,
"title": "Context"
},
"selector": {
"title": "Selector",
"type": "string"
}
},
"required": [
"selector"
],
"title": "click_elementArguments",
"type": "object"
}
Implementation Reference
- src/mcp_web_browser/server.py:169-202 (handler)The main handler function for the 'click_element' tool, decorated with @mcp.tool() for registration. It uses Playwright to locate and click an element on the current page using the provided CSS selector.@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}")