playwright_click_text
Automate clicking elements on a webpage by matching their text content using the MCP Playwright Server for browser interactions.
Instructions
Click an element on the page by its text content
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | Text content of the element to click |
Implementation Reference
- src/playwright_server/server.py:270-280 (handler)The ClickTextToolHandler class that executes the playwright_click_text tool. It clicks on the first visible element matching the given text using Playwright locator.class ClickTextToolHandler(ToolHandler): @update_page_after_click async def handle(self, name: str, arguments: dict | None) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: if not self._sessions: return [types.TextContent(type="text", text="No active session. Please create a new session first.")] session_id = list(self._sessions.keys())[-1] page = self._sessions[session_id]["page"] text = arguments.get("text") await page.locator(f"text={text}").nth(0).click() return [types.TextContent(type="text", text=f"Clicked element with text {text}")]
- JSON Schema definition for the input parameters of the playwright_click_text tool, requiring a 'text' string.types.Tool( name="playwright_click_text", description="Click an element on the page by its text content", inputSchema={ "type": "object", "properties": { "text": {"type": "string", "description": "Text content of the element to click"} }, "required": ["text"] } ),
- src/playwright_server/server.py:334-344 (registration)The tool_handlers dictionary registers the ClickTextToolHandler() instance for the key 'playwright_click_text', used by the call_tool handler.tool_handlers = { "playwright_navigate": NavigateToolHandler(), "playwright_screenshot": ScreenshotToolHandler(), "playwright_click": ClickToolHandler(), "playwright_fill": FillToolHandler(), "playwright_evaluate": EvaluateToolHandler(), "playwright_click_text": ClickTextToolHandler(), "playwright_get_text_content": GetTextContentToolHandler(), "playwright_get_html_content": GetHtmlContentToolHandler(), "playwright_new_session":NewSessionToolHandler(), }
- Decorator applied to the handler's handle method to automatically update the session's page reference after a click event, handling potential new page loads.def update_page_after_click(func): async def wrapper(self, name: str, arguments: dict | None): if not self._sessions: return [types.TextContent(type="text", text="No active session. Please create a new session first.")] session_id = list(self._sessions.keys())[-1] page = self._sessions[session_id]["page"] new_page_future = asyncio.ensure_future(page.context.wait_for_event("page", timeout=3000)) result = await func(self, name, arguments) try: new_page = await new_page_future await new_page.wait_for_load_state() self._sessions[session_id]["page"] = new_page except: pass # if page.url != self._sessions[session_id]["page"].url: # await page.wait_for_load_state() # self._sessions[session_id]["page"] = page return result return wrapper