click_element
Click buttons, links, or interactive elements on web pages to navigate interfaces, open modals, start conversations, or trigger UI actions using CSS selectors or visible text targeting.
Instructions
Click a button, link, or any interactive element on the page. Useful for navigating through multi-step interfaces, opening chat modals, starting new conversations, or triggering UI actions. Can target elements by CSS selector or by their visible text content. Automatically waits after clicking to allow page updates.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| selector | No | CSS selector for the element to click (e.g., 'button#start-chat', '.new-conversation-btn'). Use this when you know the exact selector. | |
| sessionId | Yes | Session ID obtained from initialize_session | |
| text | No | Alternative to selector: visible text content to search for and click (e.g., 'Start Chat', 'Sign In', 'New Conversation'). Use this when selector is unknown. | |
| waitAfter | No | Milliseconds to wait after clicking to allow animations, redirects, or dynamic content to load (default: 1000) |
Implementation Reference
- src/tools/interaction.js:11-59 (handler)The core handler function `clickElement` that executes the tool logic: clicks elements by CSS selector or text content using Playwright, handles errors, waits after click, and returns success status with page info.export async function clickElement( sessionId, selector = null, text = null, waitAfter = 1000 ) { const session = getSession(sessionId); const { page } = session; try { if (selector) { await page.click(selector); } else if (text) { // Try to find element by text content const clickedByText = await page.evaluate((searchText) => { const elements = Array.from( document.querySelectorAll('button, a, [role="button"], [onclick]') ); const target = elements.find((el) => el.textContent.trim().toLowerCase().includes(searchText.toLowerCase()) ); if (target) { target.click(); return true; } return false; }, text); if (!clickedByText) { // Try using Playwright's text selector await page.click(`text=${text}`); } } else { throw new Error("Either selector or text parameter is required"); } await page.waitForTimeout(waitAfter); return { success: true, sessionId, currentUrl: page.url(), title: await page.title(), message: `Clicked element successfully`, }; } catch (error) { throw new Error(`Failed to click element: ${error.message}`); } }
- src/index.js:126-155 (schema)The tool schema definition for 'click_element', including inputSchema for parameters (sessionId, selector, text, waitAfter) used for validation in ListToolsRequest.name: "click_element", description: "Click a button, link, or any interactive element on the page. Useful for navigating through multi-step interfaces, opening chat modals, starting new conversations, or triggering UI actions. Can target elements by CSS selector or by their visible text content. Automatically waits after clicking to allow page updates.", inputSchema: { type: "object", properties: { sessionId: { type: "string", description: "Session ID obtained from initialize_session", }, selector: { type: "string", description: "CSS selector for the element to click (e.g., 'button#start-chat', '.new-conversation-btn'). Use this when you know the exact selector.", }, text: { type: "string", description: "Alternative to selector: visible text content to search for and click (e.g., 'Start Chat', 'Sign In', 'New Conversation'). Use this when selector is unknown.", }, waitAfter: { type: "number", description: "Milliseconds to wait after clicking to allow animations, redirects, or dynamic content to load (default: 1000)", default: 1000, }, }, required: ["sessionId"], }, },
- src/index.js:441-457 (registration)Registration in the tool dispatcher switch statement: handles CallToolRequest for 'click_element', validates params, and invokes the clickElement handler.case "click_element": { const { sessionId, selector, text, waitAfter = 1000 } = args; if (!sessionId) { throw new McpError( ErrorCode.InvalidParams, "sessionId parameter is required" ); } if (!selector && !text) { throw new McpError( ErrorCode.InvalidParams, "Either selector or text parameter is required" ); } result = await clickElement(sessionId, selector, text, waitAfter); break; }
- src/index.js:14-27 (registration)Import registration of the clickElement handler from reverseEngineer.js into the main index.js for use in tool handling.clickElement, fillForm, switchTab, waitForElement, navigateToUrl, getCurrentPageInfo, initializeSession, closeSession, startNetworkCapture, stopNetworkCapture, getNetworkCaptureStatus, clearNetworkCapture, } from "./tools/reverseEngineer.js";
- src/tools/reverseEngineer.js:16-17 (registration)Re-export of clickElement from interaction.js in the central tools export file.export { clickElement, fillForm, waitForElement } from "./interaction.js";