playwright_click_text
Click web page elements by their visible text content for efficient web automation. Supports precise interaction during automated workflows.
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-279 (handler)The ClickTextToolHandler class provides the core implementation of the playwright_click_text tool. It retrieves the active Playwright page session and clicks on the first element matching the provided text using a text-based 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 maps tool names to their handler instances, including 'playwright_click_text' to ClickTextToolHandler(). This is used in the @server.call_tool() handler to dispatch tool calls.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 function used by click handlers, including ClickTextToolHandler, to handle potential new page creation after a click event (e.g., navigation) by waiting for and switching to the new page.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